Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从cakePHP containable behavior find构建单个结果数组?_Php_Mysql_Cakephp - Fatal编程技术网

如何从cakePHP containable behavior find构建单个结果数组?

如何从cakePHP containable behavior find构建单个结果数组?,php,mysql,cakephp,Php,Mysql,Cakephp,型号:用户、产品、两种ActAs均可容纳 用户拥有许多属于用户的产品和产品 用户->查找“全部”后的结果: 我只想显示产品,按价格订购,与用户订单无关,但我需要一些用户信息来查看 例如,观点: ID=9的产品属于Paul,价格为10 ID=10的产品属于Susan,价格为15 ID=8的产品属于Susan,价格为22 ID=7的Prodcut属于Paul,成本为100解决方案将contain设置为false并创建自定义联接: return $this->Product->find('

型号:用户、产品、两种ActAs均可容纳

用户拥有许多属于用户的产品和产品

用户->查找“全部”后的结果:

我只想显示产品,按价格订购,与用户订单无关,但我需要一些用户信息来查看

例如,观点:

ID=9的产品属于Paul,价格为10 ID=10的产品属于Susan,价格为15 ID=8的产品属于Susan,价格为22 ID=7的Prodcut属于Paul,成本为100

解决方案将contain设置为false并创建自定义联接:

return $this->Product->find('all', array(
    'contain' => false,
    'joins' => array (
        array (
        'table' => 'users',
        'alias' => 'User',
        'type' => 'INNER',
        'conditions' => array (
            'User.id = Product.user_id'
        )
    )
    ),
    'fields' => array('User.*', 'Product.*'),
    'order' => 'Product.year ASC'           
    )); 
但是,我想知道是否有更好的办法


在model Product中,我尝试使用contain=>array'User',但出现了一个错误,用户模型与产品模型没有关联,我不理解这一点,因为我对$hasMany和$BELONGS进行了很好的定义。

如果您只想显示产品,为什么要从用户模型运行查找?您是否尝试在产品型号上运行find?否则,如果你想操纵你的数组,你应该阅读@AgRizzo,如果运行“从产品模型中查找”,我怎么能在我的视图中有/打印用户信息?我不理解你的问题。您当前正在运行“从用户模型查找”并获取产品信息。为什么您认为从产品运行find不会返回用户信息?您可以在两个方向上设置模型关联。您需要从产品模型运行find,查看结果并确定它们是否满足您的需要。我现在运行find from Products model,结果是一个包含产品的数组,没有附加用户信息,只有用户id foring键ofc@AgRizzo,查看我要执行的查询:选择User.username,Product.id,Product.user_id,Product.price FROM ddb.products AS Product,ddb.users AS user其中user.id=Product.user_id按Product.price ASCpublic$actsAs=数组“Containable”;public$BelongsTo=数组'User';变量名区分大小写。您需要使用$belong来注意小写的b
return $this->Product->find('all', array(
    'contain' => false,
    'joins' => array (
        array (
        'table' => 'users',
        'alias' => 'User',
        'type' => 'INNER',
        'conditions' => array (
            'User.id = Product.user_id'
        )
    )
    ),
    'fields' => array('User.*', 'Product.*'),
    'order' => 'Product.year ASC'           
    ));