Cakephp3:无法在具有hasmany关联的查找查询中添加条件

时间:2018-09-04 10:06:13

标签: cakephp cakephp-3.0

我无法通过条件从hasmany关联中查找条件,返回未找到列错误。如果我没有在restaurantamenities表上通过任何条件,那么它将返回数据以及多个restaurantamenities记录。请检查以下代码,让我知道问题出在哪里。我已经附上了实体,模型和控制器的代码。其他所有东西都在这里工作。

实体:餐馆

class Restaurant extends Entity
{
    protected $_accessible = [
        'user_id' => true,
        'name' => true,
        'image' => true,
        'description' => true,
        'created' => true,
        'modified' => true,
        'user' => true,
        'restaurant_amenities'=>true
    ];
}

实体RestaurantAmenities

class RestaurantAmenities extends Entity
{
    protected $_accessible = [
        'restaurant_id' => true,
        'amenity_id' => true,
        'created' => true,
        'modified' => true,
        'restaurants' => true
    ];
}

表:餐厅表

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

        $this->setTable('restaurants');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);
        $this->hasMany('RestaurantAmenities', [
            'className' => 'restaurant_amenities',
            'foreignKey' => 'restaurant_id'
        ])->setJoinType('INNER')->setDependent(true);

    }

RestaurantAmenities表

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

        $this->setTable('restaurant_amenities');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsTo('Amenities', [
            'foreignKey' => 'amenity_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Restaurants', [
            'foreignKey' => 'restaurant_id',
            'joinType' => 'INNER'
        ]);

    }

在RestaurantsController上查找查询

$distanceField = '(3959 * acos (cos ( radians(:latitude) )
                    * cos( radians( Restaurants.latitude ) )
                    * cos( radians( Restaurants.longitude )
                    - radians(:longitude) )
                    + sin ( radians(:latitude) )
                    * sin( radians( Restaurants.latitude ) )))'; //* 1.60934
            $amenities = [2,3];
            $restaurants = $this->Restaurants->find()
                    ->select(['Restaurants.id','Restaurants.name','Restaurants.image','Restaurants.avg_rating','Restaurants.starting_price','Cuisines.name','RestaurantAmenities.amenity_id',
                        'distance' => $distanceField
                    ]) ->having(["distance < " => $distance])
                    ->bind(':latitude', $latitiude, 'float')
                    ->bind(':longitude', $longitude, 'float')
                    ->contain(['RestaurantAmenities'=>function($query) use($amenities){
                         return $query->select(['amenity_id'])
                        ->where(['RestaurantAmenities.amenity_id IN' => $amenities]);    

                        },'Users'=>function($q){
                            return $q->where(['Users.status'=>true]);
                        }])
                    ->where($conditions)->toArray();

找不到列返回错误,而表中存在该列:

{ "message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column \u0027RestaurantAmenities.amenity_id\u0027 in \u0027field list\u0027", "url": "\/api\/restaurants\/filter.json", "code": 500, "file": "\/home\/simerjit\/public_html\/supperout\/vendor\/cakephp\/cakephp\/src\/Database\/Statement\/MysqlStatement.php", "line": 39 }

0 个答案:

没有答案