【问题标题】:create proper association in cakephp 3在 cakephp 3 中创建适当的关联
【发布时间】:2017-12-21 21:53:08
【问题描述】:

我想创建适当的 b/w 两个表的关系。这样当我从lease 表中获取数据时,我也将能够获取price 表数据。价格表可以有多个 id 为lease 表。

表的结构是

租用表

价格表

其中idlease 表与价格表的lease_id 相关。

我已尝试使用以下代码,但无法获取数据。

class LeasesTable extends Table {

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config) {
        parent::initialize($config);

        $this->table('leases');

        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsTo('Offices', [
            'foreignKey' => 'office_type',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Countries', [
            'foreignKey' => 'country_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Areas', [
            'foreignKey' => 'area_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsToMany('id', [
            'foreignKey' => 'lease_id',
            'joinType' => 'INNER',
            'joinTable' => 'Prices',
        ]);
    }

【问题讨论】:

    标签: php mysql cakephp model cakephp-3.0


    【解决方案1】:

    您描述的关联是 Lease 有很多价格 - 所以您应该在 LeasesTable.php 中使用以下代码:

    $this->hasMany("Prices", [
        "foreignKey" => "lease_id"
    ]);
    

    更多信息:HasMany Associations

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多