提问者:小点点

如何遍历CakePHP关系?


我可以访问产品属性(product-

这是我的密码:

//Product Model
class Product extends AppModel {
    public $useTable = 'products';
    public $displayField = 'name';
    public $hasAndBelongsToMany = array(
        'Attributes' =>
            array(
                'className' => 'Attribute',
                'joinTable' => 'product_has_attributes',
                'foreignKey' => 'products_id',
                'associationForeignKey' => 'attributes_id',
                'unique' => true,
                'conditions' => '',
                'fields' => '',
                'order' => '',
                'limit' => '',
                'offset' => '',
                'finderQuery' => '',
                'with' => 'product_has_attributes'
            )
    );
    public $hasMany = array(
        'Ads' => array(
            'className' => 'Ad',
            'foreignKey' => 'Products_id',
            'conditions' => '',
            'order' => '',
            'limit' => '',
            'dependent' => true
        )
    );
//Ad Model
class Ad extends AppModel {
    public $displayField = 'Name';
    public $belongsTo = array(
        'Product' => array(
            'className' => 'Products',
            'foreignKey' => 'products_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
//Attribute Model
class Attribute extends AppModel {
    public $displayField = 'name';
    public $hasAndBelongsToMany = array(
        'Products' =>
            array(
                'className' => 'Product',
                'joinTable' => 'product_has_attributes',
                'foreignKey' => 'attributes_id',
                'associationForeignKey' => 'products_id',
                'unique' => true,
                'conditions' => '',
                'fields' => '',
                'order' => '',
                'limit' => '',
                'offset' => '',
                'finderQuery' => '',
                'with' => 'product_has_attributes'
            )
    );
        
// Products controller -> Action View
class ProductsController extends AppController {
    public function view($id = null) {
        if (!$this->Product->exists($id)) {
            throw new NotFoundException(__('Invalid product'));
        }
        $options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
        $this->set('product', $this->Product->find('first', $options));
    }
}

// Ads controller -> Action View
class AdsController extends AppController {
    public function view($id = null) {
        if (!$this->Ad->exists($id)) {
            throw new NotFoundException(__('Invalid ad'));
        }
        $options = array('conditions' => array('Ad.' . $this->Ad->primaryKey => $id));
        $this->set('ad', $this->Ad->find('first', $options));

    }

以下是我在视图中所做的:

//Products Views: snipet of view.ctp
print_r ($product); 
// this prints rhe product and all associated attributes

//Ads Views: snipet of view.ctp
print_r ($ad['Product']); 
//this will print only the product fields, but not the attributes associated to the product

怎么了?如何访问关系Ad-


共2个答案

匿名用户

我可以想出几个简单的方法来实现这一点。

第一:

class AdsController extends AppController {
    if (!$this->Ad->exists($id)) {
        throw new NotFoundException(__('Invalid ad'));
    }
    $options = array(
        'conditions' => array('Ad.' . $this->Ad->primaryKey => $id),
        'recursive' => 1, // or more
    );
    $this->set('ad', $this->Ad->find('first', $options));
}

这将是确保获得与返回的产品相关的属性的最简单的代码更改。

否则,你在问题中提到了这一点。您可以通过模型访问相关模型,因此:

$this->Ad->Product->find(...);

当在条件中使用相关模型时,我对Cake不喜欢我的查找条件有意见,我不确定正确的语法,但是如果你想追求它,我相信你可以通过文档或实验来追踪它。

最后,我建议您检查容器行为,这将允许您微调结果中实际返回的字段。我希望这回答了你的问题!

匿名用户

也许问题在于您没有使用CakePHP的约定。

从文档中:

新联接表的名称需要包括所涉及的两个模型的名称,按字母顺序排列,并用下划线(33;)分隔

因此,您的联接表应该命名为attributes\u products

另外,检查你的外键。它们应该是单数形式。

  • 产品属性。id

希望这能解决问题。

参考文献:

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm

http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions