how to work with foreing key

34 views
Skip to first unread message

nicolas potier

unread,
Jan 11, 2013, 10:16:44 AM1/11/13
to pommp...@googlegroups.com
Hi,

I've read in the V1.1 documentation that filters can be used on Collection to create relation between entities :

    return $this->query($sql, array($slug))
        ->registerFilter(array($author_map, 'createFromForeign'))
        ->current();

I'm getting an error :  Function name must be a string in [path]\vendor\pomm\pomm\Pomm\Object\Collection.php on line 101

I'm thinking that registerFilter works with anonymous functions, but not with an array.

Collection.php 

foreach ($this->filters as $index => $filter) {
            $values = $filter($values);
            if (!is_array($values)) {
                throw new Exception(sprintf("Filters have to return an Array. Filter number %d returned a '%s'.", $index, gettype($values)));
            }
 
What im I missing ?

nicolas potier

unread,
Jan 11, 2013, 12:14:01 PM1/11/13
to pommp...@googlegroups.com
Hi,

I've been able to make the code given in documentation by editing the method get of the class Collection given in Pomm.

    public function get($index)
   
{
       
if (isset($this->fetched[$index])) {
           
return $this->fetched[$index];
       
}


        $values
= $this->stmt->fetch(\PDO::FETCH_ASSOC, \PDO::FETCH_ORI_ABS, $index);


       
if ($values === false)
           
return false;

       
       
foreach ($this->filters as $index => $filter) {

           
if (is_array($filter)) {
                $values
= $filter[0]->$filter[1]($values);
           
}
           
else {

                $values
= $filter($values);
           
}
           
if (!is_array($values)) {
               
throw new Exception(sprintf("Filters have to return an Array. Filter number %d returned a '%s'.", $index, gettype($values)));
           
}
       
}



        $fetched
= $this->object_map->createObjectFromPg($values);
        $this
->fetched[] = $fetched;


       
return $fetched;
   
}

This is a quick fix, but any explaination is welcome :)

Nicolas.

Grégoire Hubert

unread,
Jan 12, 2013, 3:08:07 AM1/12/13
to pommp...@googlegroups.com
You are not missing anything, that's a bug. Pomm should check if the filter is a callable and if it is, then triggers it with something like this :

    if (is_callable($callable))
    {
        call_user_func($callable);
    }
    else
    {
        throw new PommException(...)
    }
 
If you feel confident, submit a PR (with the according test case) ;) Otherwise, I will do it at the end of this week end.

Cheers,
--
Grégoire

Nicolas Potier

unread,
Jan 12, 2013, 5:11:01 AM1/12/13
to pommp...@googlegroups.com
Hi Grégoire,

I get it. Indeed, there should be a check or something. But the fact is that the arguments given in the addFilter method have different structure (well at least in the current documentation):
  • in on case it's an anonymous function
  • in an other case it's an array with the Object map and the method to call.
As a consequence, the filters array contained in the Collection Object has different kind of data. As I said in another message, I've been able to make the code given in documentation by editing the method get of the class Collection given in Pomm :

        foreach ($this->filters as $index => $filter) {

            
if (is_array($filter)) {

                $values 
= $filter[0]->$filter[1]($values);
            
}
            
else {

                $values 
= $filter($values);
            
}


Do you think this test should be maintained and completed with an is_callable test or something else should be done ?

Nicolas

-- 
Nicolas Potier
Envoyé avec Sparrow

--
You received this message because you are subscribed to the Google Groups "PommProject" group.
Visit this group at http://groups.google.com/group/pommproject?hl=en.
 
 

Grégoire Hubert

unread,
Jan 12, 2013, 6:47:30 AM1/12/13
to pommp...@googlegroups.com
On Sat, Jan 12, 2013 at 11:11 AM, Nicolas Potier <nicolas...@acseo-conseil.fr> wrote:
Hi Grégoire,

I get it. Indeed, there should be a check or something. But the fact is that the arguments given in the addFilter method have different structure (well at least in the current documentation):
  • in on case it's an anonymous function
  • in an other case it's an array with the Object map and the method to call.

Anonymous functions and array with an instance and a method names are both callable to PHP. See http://php.net/manual/en/language.types.callable.php so the code I have provided should do the job.

Cheers,
--
Grégoire HUBERT
Pomm non ORM http://pomm.coolkeums.org

Grégoire Hubert

unread,
Jan 12, 2013, 10:42:00 AM1/12/13
to pommp...@googlegroups.com
Hi,

The lastest commit on both branches fixes the issue (see #70).

Thank you for your feedback :)

Nicolas Potier

unread,
Jan 13, 2013, 7:19:04 AM1/13/13
to pommp...@googlegroups.com
Great thank you :) You was faster than me. 


-- 
Nicolas Potier
Envoyé avec Sparrow

--
Reply all
Reply to author
Forward
0 new messages