Answer the question
In order to leave comments, you need to log in
What is the correct way to select with hasMany()?
Good afternoon,
I organized a connection in the database and generated hasMany () in the model
class Day extends \yii\db\ActiveRecord
{
public function getXxx()
{
return $this->hasMany(\app\models\xxx::className(), ['days_id' => 'id']);
}
\app\models\Day::getXxx();
Calling unknown method: app\modules\caregiver\controllers\MyController::hasMany()
Answer the question
In order to leave comments, you need to log in
1) Relationships are declared at two ends example:
class Customer extends ActiveRecord
{
public function getOrders()
{
return $this->hasMany(Order::className(), ['customer_id' => 'id']);
}
}
class Order extends ActiveRecord
{
public function getCustomer()
{
return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
}
}
// SELECT * FROM `customer` WHERE `id` = 123
$customer = Customer::findOne(123);
// SELECT * FROM `order` WHERE `customer_id` = 123
// $orders - это массив объектов Order
$orders = $customer->orders;
$customer->orders; // массив объектов `Order`
$customer->getOrders(); // объект ActiveQuery
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question