简体   繁体   English

在Lithium中访问多个模型深层关系

[英]Accessing more than one model deep relationships in Lithium

Is it possible to access more than one model deep in a relationship in Lithium? 是否可以在Lithium的关系中访问多个模型?

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");
}

and a State model, and so on. 和国家模型,等等。

If I'm querying for a User, with something similar to Users::first() , is it possible to get all the relationships included with the results? 如果我正在查询用户,使用类似于Users::first() ,是否可以获得结果中包含的所有关系? I know I can do Users::first(array('with' => 'City')) but I'd like to have each City return its State model, too, so I can access it like this: 我知道我可以做Users::first(array('with' => 'City'))但是我想让每个City都返回它的State模型,所以我可以像这样访问它:

$user->city->state->field

Right now I can only get it to go one deep ( $user->city ) and I'd have to requery again, which seems inefficient. 现在我只能让它深入( $user->city )并且我必须再次重新查询,这似乎效率低下。

Using a recent master you can use the following nested notation: 使用最近的master,您可以使用以下嵌套表示法:

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

It will do the JOINs for you. 它会为你做JOIN。

I am guessing you are using SQL? 我猜你在使用SQL?

Lithium is mainly designed for noSQL db´s, so recursiveness / multi joins are not a design goal. Lithium主要是为noSQL db而设计的,因此递归/多连接不是设计目标。

  • You could set up a native sql join query and cast it on a model. 您可以设置本机sql连接查询并将其强制转换为模型。
  • query the city with Users and State as joins. 使用Users和State查询城市作为连接。
  • you could setup a db based join view and li3 is using it as a seperate model. 您可以设置基于数据库的连接视图,li3将其用作单独的模型。
  • you probably should split your planned recursive call into more than one db requests. 您可能应该将计划的递归调用拆分为多个数据库请求。

Think about the quotient of n Cities to m States. 想想n个城市对m个国家的商数。 => fetch the user with city and then the state by the state id. =>使用city获取用户,然后按州id获取状态。 => pass that as two keys or embed the state info. =>将其作为两个键传递或嵌入状态信息。 This would be acceptable for Users::all() queries aswell. 这对于Users :: all()查询也是可以接受的。

Example using Lithiums util\\Set Class: 使用Lithiums util \\ Set类的示例:

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
    ),
));

You can set up relationships in this way, but you have to use a more verbose relationship definition. 您可以通过这种方式设置关系,但必须使用更详细的关系定义。 Have a look at the data that gets passed when constructing a Relationship for details 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 the States relationship on Users, be sure to always include the Cities relationship in the same query. 在用户上使用States关系时,请务必始终在同一查询中包含Cities关系。 For example: 例如:

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

I have never tried this using belongsTo relationships, but I have it working using hasMany relationships in the same way. 我从来没有尝试过使用belongsTo关系,但我让它以同样的方式使用hasMany关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM