I got this working in Cake although I'm not sure it's better than
making a direct SQL query with a function. In my parent_table's model:
function afterFind($results)
{
$results['relname'] = $this->ParentTable-
>_getRelname($results['id']);
return $results;
}
function _getRelname($id)
{
$sql = 'SELECT p.relname FROM parent_table AS pt '
. 'LEFT JOIN pg_catalog.pg_class AS p ON p.oid = pt.tableoid '
. "WHERE pt.id = ${id}";
$result = $this->ParentTable->query($sql);
return $result[0][0]['relname'];
}
A few questions:
1) Why does query() return such a deeply-nested array? If the first
level holds each row what could be the use of another level?
2) On the same subject, is there some way to use Set::extract() to
remove a level from these kinds of arrays? I've tried all sorts of
things without any results.
3) Similarly, the results passed to afterFind() can be nested in
different ways. That seems reasonable to me but I'm not sure that I'm
doing things quite correctly. My afterFind() actually looks like this:
function afterFind($results)
{
if (isset($results['id']))
{
$results['relname'] = $this->ParentTable-
>_getRelname($results['id']);
return $results;
}
foreach ($results as $key => $val)
{
if (array_key_exists($this->name, $val) && isset($results[$key]
[$this->name]['id']))
{
$results[$key][$this->name]['relname'] =
$this->ParentTable-
>_getRelname($results[$key][$this->name]['id']);
}
else if (isset($results[$key]['id']))
{
$results[$key]['relname'] = $this->ParentTable-
>_getRelname($results[$key]['id']);
}
}
return $results;
}
That's based on something I found online (although, I had to add the
"&& isset($results[$key][$this->name]['id'])" to make it work). It's a
bit repetitive. Is there a better way?
4) Why is it that the model is not available in $this in model
methods? When I realised that $this points to the controller in
afterFind() I just rolled my eyes and accepted it. But it seems
completely strange that it's the same in _getRelname(). What's up with
that?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---