I
I
Igor Vasiliev2021-03-19 22:52:40
API
Igor Vasiliev, 2021-03-19 22:52:40

Yii2, how to insert into sql query coalesce field, array from API?

Hello.
--
My background:
There is a third-party API - https://www.bridgeinteractive.com
Documentation - https://rets.ly/docs/platform/
This API simply outputs property data as json
There is a Yii2 application that has its own API which also outputs data in the form of json
I'm trying to combine the SQL query from ActiveRecord with the json received from another API.
Maybe I'm doing something wrong, or it's impossible to do this, because you can't make a connection with the Bridge API.
During a collision, I only get a key, by which I can then pull out real estate data.
It turns out a view of all deals, and getting one specific deal that gives data in the form of JSON.
The problem is that I need to make two requests for two APIs, and already at the front, link the two received json into one.

For you to understand, I can set an SQL query, something like:

SELECT d.*,  coalesce(o.key, 0) as realestate FROM deals ...

That is, create an additional field (column) that is not in the database
(I'm not talking about a query now, how to create a column in the database)
So o.key === Bridge API, if there is no data, and to avoid errors, I fill the empty cell with zero, and the column where to arrive data from the Bridge API I will call for example "realestate".
In the case of SQL in one database, such a trick is generally easy to turn, a non-existent column appears in which data from another table is entered.

As I see it. They declare that the JSON file has a header, I get all the records from the database, I get all the data from the Bridge API by key and combine them into one array, which I pass through the loop, thus combining different json into one, through the loop I display all the records already with objects (the property).

What BUT.
The fact is that in the Bridge API, the data is constantly changing, except for the key
by which you can get real estate data. As I understand it, it is updated every second, at least through the axios library in React JS, because of this, the limit is constantly running out, which cannot be said if the data comes via cURL in php.
There can be as many real estate objects as you want, at least 30,000 or 40,000.
There can also be as many transactions in the API as you like.
I'm worried about the load and the speed of getting data. Their number is growing, and who knows how much more data will be.
6054fb2820346853304538.jpeg

As you can see, this key can be used to get real estate data, and the same key is in the API of another site. How best to link this data into a single json without losing performance and speed, I don’t understand yet. Maybe someone has done something similar. Through hasOneI tried, but apparently I'm not doing something right.
public function getRealestate(){
        return $this->hasOne(...);
    }


Deals::find()->with('realestate')->where(['key' => $key])->asArray()->all();


What do you advise. Thanks in advance for your help and kindness.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question