Hi everybody! I have 2 simple models:
<?php
namespace My\Model;
class Choice extends \Phalcon\Mvc\Model {
protected id;
protected option_id;
public function initialize()
{
$this->hasOne('option_id', 'My\Model\Option', 'id', ['alias' => 'Option']);
}
}
A choice must have 1 selected option or null
(nothing selected).
<?php
namespace My\Model;
use Phalcon\Mvc\Model\Validator\PresenceOf;
class Option extends \Phalcon\Mvc\Model {
protected id;
protected $name;
public function initialize()
{
// I don't need for a "belongTo"
}
public function validation() {
$this->validate(new PresenceOf([
'field' => 'name',
'message' => $di['translator']->translate('Name can\'t be empty!')
]));
}
}
$choice = new \My\Model\Choice();
$option = \My\Mode\Option::findFirst("name = 'awesome'");
$choice->Option = $option;
$choice->save();
How can I delete an Option of an existed Choice (set null
to option_id
)?
I tried
$choice = \My\Model\Choice::findFirst(1);
$choice->Option = null;
$choice->save();
This doesn't work.
This tries to create a new empty instance of \My\Model\Option
and save it.
Of course, it throws a validation message that the name is empty.