首页 文章

我如何遍历CakePHP关系?

提问于
浏览
1

我试图遍历CakePHP模型上的关系 . 这是数据库:
My database

我可以在产品模型上访问产品属性(product-> attributes),但我无法访问Ad模型上的产品属性(ad-> product-> attributes) .

这是我的代码:

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

}

以下是我在观点中所做的事情:

//产品视图:snipet of view.ctp
print_r($ product);
//这打印出产品和所有相关属性

//广告视图:snipet of view.ctp
print_r($ ad ['Product']);
//这将仅打印产品字段,但不打印与产品关联的属性

怎么了?如何从我的广告模型访问Ad-> product->属性关系?

2 回答

  • 2

    也许问题可能是你没有使用CakePHP的约定 .

    来自文档:

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

    因此,您的连接表应命名为 attributes_products .

    另外,检查您的外键 . 它们应该是单一的形式 .

    • attributes_products.id

    • attributes_products.product_id

    • attributes_products.attribute_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

  • 1

    我可以想到几种简单的方法来实现这一目标 .

    第一:

    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不喜欢我的查找条件的问题,我不确定正确的语法,但是如果你想要追求,我相信你可以通过文档跟踪它或者通过实验 .

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

相关问题