When I try to update an entry validation fires an uniqueness error. I want to get my object by label attribute and update other nodes of object.
Here is the update code:
$shelf = Collections\ShelfCollection::findFirst(array(
array('label' => 'my-label')
));
if ($shelf) {
$shelf->value = 'changed-value';
if ($shelf->save() == false) {
foreach ($shelf->getMessages() as $message) {
echo $message, "\n";
}
} else {
echo "Shelf value updated successfully!<br />";
}
}
else {
echo 'Shelf label not found';
}
Simply you can take a look to my collection model:
<?php
namespace Collections;
use Validator\Collection\UniqueValidator As Uniqueness,
Phalcon\Mvc\Model\Message as Message;
class ShelfCollection extends \Phalcon\Mvc\Collection {
public $label;
public $value;
public function getSource()
{
return "shelf";
}
public function initialize()
{
// Auto generate _id keys
$this->useImplicitObjectIds(true);
}
public function validation()
{
$this->validate(new Uniqueness(
array(
"field" ="value",
"message" ="Value must be unique"
)
));
$this->validate(new Uniqueness(
array(
"field" ="label",
"message" ="Label must be unique"
)
));
return $this->validationHasFailed() != true;
}
public function beforeSave()
{
// May Apply Here Business Layout Model
}
}