Access to more than one lithium deep bond model

Is it possible to access more than one model in the relationship between lithiums?

For example, I have a User model:

class Users extends \lithium\data\Model { public $validates = array(); public $belongsTo = array("City"); } 

and I have a city model:

 class Cities extends \lithium\data\Model { public $validates = array(); public $belongsTo = array("State"); } 

state model etc.

If I ask a user with something similar to Users::first() , is it possible to get all the relationships included in the results? I know what I can do Users::first(array('with' => 'City')) , but I would like each City to return its state model, so I can access it like this:

 $user->city->state->field 

Right now, I can only get it to the depths ( $user->city ), and I will have to demand again, which seems ineffective.

+4
source share
3 answers

I assume you are using SQL?

Lithium is primarily intended for noSQL db's, so recursion / multiple joins are not the purpose of the design.

  • You can configure your own sql connection request and apply it to the model.
  • Query a city with users and states in the form of associations.
  • you can customize the db-based connection view, and li3 uses it as a separate model.
  • you probably should split the planned recursive call into several db requests.

Think of the coefficient of n cities for m states. => select a user with a city, and then a state with a state identifier. => pass this as two keys or insert status information. This would be acceptable also for Users :: all () requests.

Lithiums util \ Set Class example:

 use \lithium\util\Set; $users = Users::all(..conditions..); $state_ids = array_flip(array_flip(Set::extract($users->data(), '/city/state_id'))); $stateList = States::find('list',array( 'conditions' => array( 'id' => $state_ids ), )); 
+3
source

Using a recent wizard, you can use the following naming conventions:

 Users::all( array( 'with' => array( 'Cities.States' ) )); 

He will do the JOIN for you.

+9
source

You can set up relationships in this way, but you need to use a more detailed definition of relationships. Look at the data that is passed when building the Relationship for detailed information about the options you can use.

 class Users extends \lithium\data\Model { public $belongsTo = array( "Cities" => array( "to" => "app\models\Cities", "key" => "city_id", ), "States" => array( "from" => "app\models\Cities", "to" => "app\models\States", "key" => array( "state_id" => "id", // field in "from" model => field in "to" model ), ), ); } class Cities extends \lithium\data\Model { public $belongsTo = array( "States" => array( "to" => "app\models\States", "key" => "state_id", ), ); } class States extends \lithium\data\Model { protected $_meta = array( 'key' => 'id', // notice that this matches the value // in the key in the Users.States relationship ); } 

When using States relationships for users, be sure to include Cities relationships in the same query. For instance:

 Users::all( array( 'with' => array( 'Cities', 'States' ) ) ); 

I have never tried this using belongsTo relationships, but I have work using hasMany relationships in the same way.

+2
source

Source: https://habr.com/ru/post/1403036/


All Articles