Hello, in general, the essence is as follows. Yuzayu Kohana ORM. There is a table of cities in the City there is a table of users Users and a table say products Products. There is a users.city_id
connection between users and cities and, respectively, between users and products.user_id products.user_id
. The question is how to correctly know the ID of the city to pull out all the data from the Products table of related users.city_id->products.user_id
and also sorted either by product ID or by created_at
product creation field. Now in my Products table there is a city_id
field by which I pull all the data related to the city. But this is not very convenient, since when a city changes, a user has to look for all his records in the Products table and change the city_id
there. Actually the question is how to do it correctly and optimally?
Has registered such communications:
class Model_User extends Model_Auth_User { protected $_belongs_to = array( 'city' => array( 'model' => 'city', 'foreign_key' => 'city_id', ) ); protected $_has_many = array( 'cartes' => array( 'model' => 'carte', 'foreign_key' => 'user_id', ), 'roles' => array( 'through' => 'roles_users'), ); } class Model_City extends ORM { protected $_has_many = array( 'users' => array( 'model' => 'user', 'foreign_key' => 'city_id', ), ); } class Model_Carte extends ORM { protected $_belongs_to = array( 'user' => array( 'model' => 'user', 'foreign_key' => 'user_id', ), ); }
I try to pull out all the data from the Carte model that are connected to the city like this:
$city = ORM::factory('city', array('id'=>1)); foreach ($city->users->cartes->find_all() as $value) { echo $value->name.' | '; }
As a result, the void if you extract all users belonging to the city like this: $ city-> users-> find_all ()
Everything works or look for Data from the model in through a specific user also works: $ user-> find_all ()
But a bunch of City-User-Product does not work! Chyadt?
I looked at the debug and saw that the construction of the form $city->users->cartes->find_all();
Here such here sql request to a DB
"SELECT `carte`.`id` AS `id`, `carte`.`user_id` AS `user_id`, `carte`.`name` AS `name`, `carte`.`created_at` AS `created_at` FROM `cartes` AS `carte` WHERE `carte`.`user_id` IS NULL ORDER BY `created_at` DESC"
Ie in the condition user_id we have = NULL. Actually the question is generally in ORM'e Kokhanovsky possible to make chains of connections?