CakePHP3'包含'不检索数据

时间:2015-11-30 12:29:04

标签: cakephp cakephp-3.0

我正在使用CakePHP 3提供的查询构建器,使用['contain' => 'AnotherTable']从其他表中的记录中检索数据库中的“优惠”,但只检索了contain数组中的一个表,怎么能我让这两个表记录会被重新启动吗?

使用两个表进行查询(仅重新审核了'OfferBanners'):

$setting = [
            'fields' => ['id', 'product_id', 'date_start', 'date_end', 'name', 'description'],
            'contain' => ['Products', 'OfferBanners']
        ];
$offers = TableRegistry::get('Offers')
       ->find('all', $setting)->hydrate(false)->toArray();

结果数组:

Link to array pastebin

从OffersTable初始化:

public function initialize(array $config)
{
    parent::initialize($config);

    $this->table('offers');
    $this->displayField('id');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsTo('Products', [
        'foreignKey' => 'product_id',
        'joinType' => 'INNER'
    ]);
    $this->hasMany('OfferBanners', [
        'foreignKey' => 'offer_id'
    ]);
}

从ProductsTable初始化:

public function initialize(array $config)
{
    $this->table('products');
    $this->displayField('id');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsTo('Stores', [
        'foreignKey' => 'store_id',
        'joinType' => 'INNER'
    ]);
    $this->hasMany('Bookings', [
        'foreignKey' => 'product_id'
    ]);
    $this->hasMany('ProductFeatures', [
        'foreignKey' => 'product_id'
    ]);
    $this->hasMany('Offers', [
        'foreignKey' => 'product_id'
    ]);
}

表中的SQL:

CREATE TABLE products (
  id INT AUTO_INCREMENT PRIMARY KEY,
  product_name VARCHAR(255) NOT NULL,
  store_id INT NOT NULL,
  sub_category_id INT NOT NULL,
  price DECIMAL(7,2) NOT NULL,
  created DATETIME,
  modified DATETIME,
  FOREIGN KEY store_key (store_id) REFERENCES stores(id),
  FOREIGN KEY sub_category_key (sub_category_id) REFERENCES sub_categories(id)
);

CREATE TABLE offers (
  id INT AUTO_INCREMENT PRIMARY KEY,
  product_id  INT NOT NULL,
  name VARCHAR(255) NOT NULL,
  description VARCHAR(1000) NOT NULL,
  date_start DATETIME NOT NULL,
  date_end DATETIME NOT NULL,
  created DATETIME,
  modified DATETIME,
  FOREIGN KEY product_key (product_id) REFERENCES products(id)
);

CREATE TABLE offer_banners (
  id INT AUTO_INCREMENT PRIMARY KEY,
  path VARCHAR(255) NOT NULL,
  offer_id INT NOT NULL,
  created DATETIME,
  modified DATETIME,
  FOREIGN KEY offer_key (offer_id) REFERENCES offers(id)
);

1 个答案:

答案 0 :(得分:1)

$offers = TableRegistry::get('Offers');

$offers = $offers->find()
    ->select(['id', 'product_id', 'date_start', 'date_end', 'name', 'description'])
    ->select($offers->Products)
    ->select($offers->OfferBanners)
    ->contain(['Products', 'OfferBanners'])->hydrate(false)->toArray();

编辑或者,如果你正在使用cake< 3.1

$offers = TableRegistry::get('Offers');

$offers = $offers->find()
    ->select(['id', 'product_id', 'date_start', 'date_end', 'name', 'description'])
    ->select(['Products.id']) // list your Products field list here
    ->select(['OfferBanners.id']) // list your OfferBanners field list here
    ->contain(['Products', 'OfferBanners'])->hydrate(false)->toArray();

$offers = $offers->find()
    ->select(['id', 'product_id', 'date_start', 'date_end', 'name', 'description'])
    ->contain([
        'Products' => function($q) {
            return $q->autoFields(true);
        },
        'OfferBanners' => function($q) {
            return $q->autoFields(true);
        }
    ])->hydrate(false)->toArray();

如果要选择特定字段,请使用,即

   'OfferBanners' => function($q) {
            return $q->select([/* insert field list here */]);
        }