Why hasOne() is forcing update of related model? How to understand 1-1 relationships? #15685
-
Hi! I run today into the same issue: #12393 on Phalcon 3, tested it right now in Phalcon 5 and I see that it's still the same. I've created basic models: <?php
use Phalcon\Mvc\Model;
/** @property Car $car */
class Driver extends Model
{
public int $id;
public string $name;
public int|null $carId;
public int|null $alive;
public function initialize()
{
$this->useDynamicUpdate(true);
$this->setSource('drivers');
$this->hasOne(
'carId',
Car::class,
'id',
[
'alias' => 'car'
]
);
}
} <?php
use Phalcon\Mvc\Model;
class Car extends Model
{
public int $id;
public string $name;
public int|null $alive;
public function initialize()
{
$this->useDynamicUpdate(true);
$this->setSource('cars');
}
} Assigned car to Driver and decided to steal it from him later: public function indexAction()
{
/** @var Driver $driver */
$driver = Driver::findFirst(
[
'conditions' => 'id = :id:',
'bind' => [
'id' => 1
]
]
);
$car = $driver->car;
echo "Driver name is $driver->name <br>";
echo "Driver car is $car->name <br>";
$driver->carId = null;
$driver->save();
echo "Ooops! Driver doesn't have car anymore... And there are two cars now.";
exit; What I would expect is to just set carId to null, so driver doesn't have car anymore, and other driver can take it. Instead, when I hit What am I missing? A way to avoid this weird (for me) action is to avoid use of relationship $car = Car::findFirst(); The topic that I linked to is suggesting using belongsTo() instead. I don't get why and how I would use it in this example. For me Driver has (or not) one car (Because in my world Driver cannot have more than one). Driver doesn't belong to a Car. And downloading Car instead of using relationship is not what I wanted... Shouldn't this be fixed? If I want to update/save Car I will call it. For me it seems like a bug or unclear design. Regards |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Whenever you save a model, relevant related models should be saved. That's not broken. I think the issue is how you're trying to disassociate the driver from the car. I don't know the details, but Phalcon does some magic behind-the-scenes to ensure the relationship connection, and I don't think setting the connecting column to $driver->car = null;
$driver->save(); |
Beta Was this translation helpful? Give feedback.
-
shouldn't your relation be a belongsTo ? because I think so. hasOne should be used in Car model in your case $this->hasOne( |
Beta Was this translation helpful? Give feedback.
Whenever you save a model, relevant related models should be saved. That's not broken. I think the issue is how you're trying to disassociate the driver from the car. I don't know the details, but Phalcon does some magic behind-the-scenes to ensure the relationship connection, and I don't think setting the connecting column to
null
kills that connection. I think the standard way is to setcar
tonull
like: