In CakePHP, Models' main function is to represent the structure of your
database (which table a model uses, what other tables is it linked to
and how) and to query your database. Models are not Object Relational
Mappers -- that is, they don't actually keep nor represent the data you
query -- that data is returned to you as an array.
That being said, you can indeed use the 'id' field to manipulate a
single row of data. However, this is used only when manipulating your
data -- that is, updating existing records, it is not used for
querying/browsing.
In the controller, when I do:
$this->Template->id = $id;
What happens exactly ? (the debug console show no sql, so I guess
nothing much)
Indeed, nothing.
Then if I do:
$this->Template->read();
I see SQL beeing done but the function return an array. Where is the
"Object" ? How can I access it and it's method from the controller?
There is no object -- it will return an array of data and that's it.
(However you may notice that calling 'read' will set the 'id' field --
I'm not sure why this is)
How is the "Cake" way to (for example) find multiple object
(row/models) and call a function from their model?
Is it:
findAll(..) // return array of array
foreach array then call a "static" function from the model by passing
an id
That is it indeed.
$posts = $this->Post->findAll('Post.author_id' => $id);
foreach ($posts as $post) {
$this->Post->hide($post['id']);
}
or is there another way like:
getModelsObjects(..) // return an array of objects
foreach object call a public method from the object without an id.
It does not work like that by default, though this is not hard to
implement yourself. You can use the 'set' function which sets your 'id'
field (as well as data) from a row of data. Your model's 'hide' can
then check whether the 'id' field is set :
$posts = $this->Post->findAll('Post.author_id' => $id);
foreach ($posts as $post) {
$this->Post->set($post);
$this->Post->hide();
}
You will often see the 'hide' function in model 'Post' written to
catter for both usage :
function hide($id = null) {
if ($id) {
$this->id = $id;
}
if (!$this->id) return;
$this->saveField('hidden', 1);
}
So you could easilly write your own 'pullAll' function in app_model
that would simulate the same code as above :
$posts = $this->Post->pullAll('Post.author_id' => $id);
while ($this->Post->fetch()) {
$this->Post->hide();
}
though I'm not sure I would recomend this -- it seems that it could
lead to subtle problems and misunderstandings.
And from the point of View, is the model loaded from the controller
available? (if applicable)
You could pass it in yourself - however since it doesn't really
represent rows of data, there is not much need.
So if I have an action like /templates/upload/1 (1 beeing the id of the
template) how should I pass the Id back to the view html form?
(set("templateid",$id) from the controller or is there something done
for me by Cake?)
No - you should do it. However, in general, it's not the id you will
want to pass to the view, because you don't want the view to do the
querying itself.
I hope this helps !
Anselm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---