我如何遍历CakePHP关系?

hlswsv35  于 2022-11-11  发布在  PHP
关注(0)|答案(2)|浏览(150)

我尝试遍历CakePHP模型上的关系。这是数据库:

我可以访问产品型号的产品属性(product->attributes),但无法访问广告型号的产品属性(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));

    }

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

//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->product->attribute

yc0p9oo0

yc0p9oo01#

我可以想到几个简单的方法来完成这一点。
第一:

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

这将是最简单的代码更改,以确保您获得与返回的产品相关的属性。
否则,您在问题中就暗示了这一点。您可以通过模型访问相关的Model,因此:

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

当在条件中使用相关模型时,我遇到了Cake不喜欢我的查找条件的问题,我不确定正确的语法,但如果你想继续研究,我相信你可以通过文档或实验来找到它。
最后,我建议您检查ContainableBehavior,它允许您微调结果中实际返回的字段。我希望这能回答您的问题!

rfbsl7qr

rfbsl7qr2#

问题可能是您没有使用CakePHP的约定。
从文档中:
新连接表的名称需要包括所涉及的两个模型的名称,按字母顺序排列,并以下划线(_)分隔。
因此,您的连接表应命名为attributes_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

相关问题