⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Use field from another table in callback



Qarlo
  • profile picture
  • Member

Posted 11 January 2015 - 12:24 PM

Hi,

 

I have these two tables "episodes" and "seasons":

Seasons
id
number

Episodes
id
number
season_id
code

So, a season can have many episodes.

In grocery crud I set this:

$crud->display_as('number', 'Episode number');
$crud->set_relation('season_id', 'seasons', 'number');
$crud->display_as('season_id', 'Season number');

And now I would be able to update the "code" column with a callback, combining the Season number and Episode number. This is what I did:


$crud->callback_before_insert(array($this, 'add_episode_code_callback'));
$crud->callback_before_update(array($this, 'add_episode_code_callback'));

    function add_episode_code_callback($post_array)
    {
        $season_string = '' . $post_array['season_id'];
        if ($post_array['season_id'] < 10)
        {
            $season_string = '0' . $season_string;
        }

        $episode_string = '' . $post_array['number'];
        if ($post_array['number'] < 10)
        {
            $episode_string = '0' . $episode_string;
        }

        $post_array['code'] = 'S' . $season_string . 'E' . $episode_string;
        return $post_array;
    }

The problem is that it uses the season_id value instead of the "number" value from the seasons table.

Is this possible to achieve somehow?

 

 

 

 

 


titu
  • profile picture
  • Member

Posted 11 January 2015 - 13:45 PM

I guess you could query on your callback function for the Season Number. It would add just one query more, don't think is to much of an overhead.