FuelPHP ORM Update via Array - orm

The ORM in FuelPHP has an update example that looks like this:
$entry = Model_Article::find(4);
$entry->title = 'My first edit';
$entry->author = 'Total n00b';
$entry->save();
I'm wondering if there is an ability for me to update w/ something like this:
$values = array(
'this' => $that
);
Model_Article::find(4)->save($values);
The insert ability allows for passing arrays of values:
$props = array('property' => 'something');
$new = Model_Example::forge($props)->save();
But I see no example of doing the same thing w/ the update ability.
EDIT: It appears Model_Example::find(4)->values($array)->save(); is what I'm looking for.

It appears Model_Example::find(4)->values($array)->save(); is what I'm looking for.

Related

How can I get all the modified fields before saving the editing in cake php?

Using the
$entity->errors(); //is returning all errors.
to find changes before upgrading
There is something like
$entity = $this->Controller->patchEntity($entity , $this->request->data);
echo $entity->diff (); // how?
I guess you are looking for:
$entity->dirty()
If you want an array containing alla the dirty properties you can do
$entity->name = 'Foo';
$entity->description = 'Bar';
debug($entity->extract($entity->visibleProperties(), true));
you'll get
[
'name' => 'Foo',
'abbreviation' => 'Bar'
]
see the manual

Create new entity from array with Phalcon Framework

Is it possible to set Entities values automatically from array?
my entity inherets from \Phalcon\Mvc\Model and I want to set all values from array instead of querying database
is this feature already implemented?
Yes you can!
$myModel = new MyModel();
$myModel->save(['name' => 'Jeff']);
// even from post data, second argument is a whitelist of fields
$myModel->save($_POST, ['name']);
// The same for updating
$myModel = MyModel::findFirst();
$myModel->update(['name' => 'jodator']);
More here: Creating and updating records
$MyModel= new MyModel();
$MyModel->assign(array(
'id' => 1,
'name' => 'y'
));
//or
$MyModel->id = 1;
$MyModel->name = 'y';

Update join in update sql in zf2

I was trying to figure how to use Update from Zend\Db\Sql using a Join in Zend Framework 2.
In the documentation they say that the only allowed method for Update are just where() and set(), so I would like to know if there are some alternative ways to get the same results.
You could do something like this (untested):
$db = new DbAdapter(
array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=mydb;host=localhost',
'username' => 'root',
'password' => '',
)
);
$sql = 'UPDATE t1 JOIN t2 ON t1.id = t2.id SET t1.atr = 1';
$sql_result = $db->createStatement($sql )->execute();
if($sql_result->count() > 0){
echo "DONE";
}
I don't believe this is possible with the 'update()' method provided by the Zend Db Adapter.
You can, however, run the query manually using the adapter. Something like:
// $adapter is an instance of Zend_Db_Adapter
$adapter->query(YOUR QUERY HERE);
This seems to be a duplicate of Update with join using Zend-framework
Seems to be no way to do with this with Zend.

cakephp url not retrieving data

hi all when clicking the link on my page its not carrying the id from the template when going to the view page, so when the sql queries the database it is querying this
SELECT `Field`.`name`
FROM `pra`.`fields` AS `Field`
LEFT JOIN `pra`.`templates` AS `Template` ON (
`Field`.`template_id` = `Template`.`id`)
WHERE `template`.`id` IS NULL
the database says id should be = 2
here is the code for the view function
$fields = $this->Field->find('all',
array('fields'=>array('name','template_id'),
'conditions' => array('template_id' => $this->Auth->user('template.id'))));
$this->set('field', $fields);
updated code, the template_id still equals null
when hardcoded it works correctly, there is a problem with this line $this->Auth->user
You can try with the following code:
$fields = $this->Field->find('all',
array('fields'=>array('name'),
'conditions' => array('Field.template_id' => $this->Auth->user('template_id'))
)
);
$this->set('field', $fields);
Please be sure there must have any template_id value should be there for the current logged in user.
Kindly ask if it not worked for you.
Check the result of the find call, by doing a debug:
debug($fields);
This will show you the returned data from the query. You can add this to the end of your action method.
If the results are empty, double check the values that are stored in the session Auth key. You can do this by dumping out the session with debug($_SESSION) or use the CakePHP DebugKit. The Debug Kit offers you a small toolbar at the top right of the screen and lets you view session information and such.
function view($name){
$this->set('title_for_layout', 'Create Template');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.jpg');
$this->layout='home_layout';
$fields = $this->Template->Field->find('list',array(
'fields'=> array('name'),
'conditions' => array(
'template_id'=> $name)));
$this->set('field', $fields);
}
it wasn't passing the param's value

zend retriving tag list

I have some problem with zend. Here it is. I'm going to make some kind of articles db, which containt some info. Every article is marked with 1 or more tags (like WordPress).
I have a controller (let it be index) and action (also index).
All I need is to get articles and tags, associated with it, when user goes to site/index/index.
I have 3 tables:
articles(idarticles, title..)
tags(idtags, title)
tagList(idarticles, idtags).
How can I read tags, associated with article?
Zend's MVC doesn't actually include a model, however, the quickstart guide outlines creating a model.
The simplest way (not necessarily the best way), is to setup the connection in your application.ini, or setup the adapter like this (see the Zend_Db_Adapter docs):
$db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname' => 'test'
));
Then use SQL to select your data.
//all articles
$articles = $db->query('SELECT * FROM articles');
//a article's tags
$tags = $db->query('SELECT * FROM tagList JOIN tags ON
(tagList.idtag = tags.idtags) WHERE idarticles = ?', $idarticles);
This is also taged for Zend_Db_Table, to use that to access the data, first setup a default adapter (or again, use application.ini):
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Then get objects for you tables like this:
$ariclesTable = new Zend_Db_Table('articles');
To get all the articles:
$articles = $articlesTable->fetchAll();
To get an article's tags (little more complex here, using a Zend_Db_Table_Select as recommended):
$select = $tagsTable->select();
//3rd argument must be empty array, so no joined columns are selected
$select->join('tagList', 'tagList.idtag = tags.idtags', array());
$select->where('tagList.idarticles = ?', $idarticles);
$tags = tagsTable->fetchAll($select);