Hi,
how can i update 1 table field:
$media->assign(array(
'usersId' => $id,
'image_1' => $uploadDir
));
In this table are image_ 1 - image_4 fields.
Rgds
Stefan
|
Sep '15 |
4 |
2145 |
0 |
SImple solution You need to find your database object isntead of tyring to instantiate a "new" one and overwrite it.
public function updateImageAction($id){
$update = Model::findFirstById($id);
if ($update){
$update->assign(array(
'image_1' => 'updatedimage.png'
));
if (!$update->save()){
return $update->getMessages();
} else {
return true
}
}
}
More complex solution i use to update an abritrary rield.
Controller
public function ajaxUpdateFieldAction()
{
$this->view->disable();
if($this->request->isPost()){
$data = $this->request->getPost();
$recruit = Recruits::findFirstById($data['id']);
$recruit->updateField($data['field'], $data['value']);
if ($recruit){
echo json_encode($recruit->getMessages());
} else {
echo json_encode(true);
}
}
}
Model
public function updateField($field,$value)
{
$this->assign(array(
$field => $value,
'updated_date' => date('Y-m-d H:i:s'),
'last_seen' => date('Y-m-d H:i:s')
));
return $this->save();
}
You are running into the "Phalcon doesn't like blank model fields" issue.
What version of phalcon are you running on? As of phalcon 2.0.2 this update was put in. Now Mvc\Model checks if an attribute has a default value associated in the database and ignores it from the insert/update generated SQL which allows you to set default null values in the database that will bypass this issue.
The second solution. would be to set a beforeSave on your model to set "optional fields to blank or 0 values. https://forum.phalcon.io/discussion/747/what-is-the-correct-way-to-set-default-value-for-not-null-
use Phalcon\Db\RawValue;
public function beforeSave()
{
$optional = ['image_1','image_2','image_3','image_4'];
foreach($optional as $option){
if (!isset($this->$option){
$this->$option = new RawValue('default');
}
}
}
I suggest the first solution.
Hi Christian,
i am using phalcon 2.0.6 on xampp.
Now i set the default to NULL, but it doesn't work. I also tried to set a default value like "no image".
I put the second solution into model Media.php, the same issue.
Media.php
<?php
namespace Vokuro\Models;
use Phalcon\Mvc\Model;
class Media extends Model{
public $id;
public $usersId;
public $image_1;
public $image_2;
public $image_3;
public $image_4;
public function initialize(){
$this->belongsTo('usersId', 'Vokuro\Models\Users', 'id', array(
'alias' => 'user'
));
}
}
ProfilesController.php
public function image1Action(){
$media = new Media();
$media->assign(array(
'usersId' => $id,
'image_1' => $uploadDir
));
if (!$media->update()) {
$this->flash->error($media->getMessages());
} else {
$this->flash->success("Profile was created successfully");
}
return $this->response->redirect('profiles/');
}
Maybe that helps :-)
Rgds
Stefan
If you have an existing row in the db, first fetch it with MyModel::findFirst(...)
, then update the field you want to change, then save it. Otherwise, use raw SQL with $this->db->execute(...)
public function image1Action(){
$media = Media::findFirst(array('conditions'=>"usersId=?0",'bind'=>array($id)));
$media->assign(array(
'image_1' => $uploadDir
));
if (!$media->update()) {
$this->flash->error($media->getMessages());
} else {
$this->flash->success("Profile was created successfully");
}
return $this->response->redirect('profiles/');
}
Or with raw SQL:
public function image1Action(){
$res = $this->db->execute("UPDATE media SET image_1 = ? WHERE usersId = ?",array($uploadDir,$id));
if (!$res) {
$this->flash->error("Something went wrong...");
} else {
$this->flash->success("Profile was created successfully");
}
return $this->response->redirect('profiles/');
}
I know it's not the most desirable, because it takes an extra SELECT for each UPDATE... so you could give this a try: https://forum.phalcon.io/discussion/606/update-just-one-model-property
Hi Lajos, hi Christian,
@lajosbencz, i tried the solution with the overwrite save(), it didn't work, the raw SQL solution was the lucky one :-)
@christianallred, the RawValue also didn't work, but thank you for your help.
I am a absolutely beginner, maybe i did something wrong, but i found my solution.
Thx again :-)))