Closed aat2703 closed 7 years ago
It's not a bug. You just don't understand for what is hasOne
. There is sample:
User table
id | profile_id
On user Model:
public function initialize
{
$this->belongsTo('profile_id', 'Profile', 'id', ['alias' => 'profile']);
}
Profile table
id | first_name | last_name
Profile model:
public function initialize()
{
$this->hasOne('id', 'User', 'profile_id', ['alias' => 'user']);
}
So - hasOne just works same as hasMany, just returns only one model instead of resultset. It is made for unidirectional relationships. In your case since you have bidirectional relationships there should be used belongsTo in both cases.
Also it's really BAD to call productID the same as field and related field.
@Jurigag, i get it now!
Thank you!
Sorry for the naming conventions, i have abit of reading to do: https://launchbylunch.com/posts/2014/Feb/16/sql-naming-conventions/
hasOne foreignKey restriction
Hi Phalconists
I'm trying to create a 1-1 foreignKey restriction between the RestaurantOrderLines and RestaurantProducts. But if we just for fun change the hasOne function call to belongsTo then it works perfectly :)
So that an RestaurantOrderLines cannot be created if the product does not exists.
Code executed:
RestaurantOrderLines Model:
RestaurantProducts Model:
Table schema
Details