new has_many model relationships property

edited February 2013 in Feature Requests
This is a question really, though it could be a feature request. First let me say that the new has_many feature is one of the coolest things to come down the pike in some time! So here's the situation:

I'm working on a games site where people can log in and customize their own games based on ones that are available in the CMS. A fuel_user is created in the CMS and related to a table called 'clients'. When the new user is created in the 'fuel_users' table an on_after_save is used to create a record in the 'clients table' where the client enters some additional info after logging in.

So, the 2 keys in the clients table are `id` and `fuel_user_id`. A `client_id` key is stored in the 'client_games' table as well as the 'prizes' table where prize records are created by clients to offer players of their games. I'd like to be able to use the has_many feature in the client_games_model to create a multi-select of the prizes containing only the `client_id` key.

You mention in the 1.0 user_guide that a WHERE can be added.

Since this is placed at the top of the model like this...
class Client_games_model extends Base_module_model { public $has_many = array('attributes' => array('prizes' => array(FUEL_FOLDER => 'prizes_model', 'where' => 'client_id = <client_id variable>')); function __construct() { parent::__construct('client_games'); // table name } }
...how would I get the `client_id`in there? Is there another way to accomplish this? There is another related table that doesn't need to be filtered and I'm using has_many for it. However, for the prizes, I had actually created a $has_and_belongs_to_many the old way but since I had to add the on_after_save for it, I can't use the new coolness of has_many.

You mentioned the following in the user_guide:
Note that both the has_many and belongs_to relationships use the on_after_save model hook to store relationship information. So, if you overwrite this method in your model and have one of these relationships assigned to your model, you must call parent::on_after_save($values); to properly save the relationship information.
If that's what I need to do in this case, from where exactly do I call parent::on_after_save($values)? The form_fields function? Also, how would I create more than one has_many relationship for multiple tables?

Comments

  • edited 7:46PM
    Unfortunately, there is no "placeholder" value at the moment that you can use (e.g. 'client_id' = '{client_id}'. I think we know where to add it but just haven't yet. If you are interested, it would be in MY_Model on lines 2732 and 2747 in the form_fields method. There would need to be some logic to test if the where is a string or an array and then use something like a preg_replace_callback to mix in the real values from the $values array.

    An alternative would be to try setting the has_many property in your model's form_fields method right before you call parent::form_fields($values); This gives you the $values array which has the client_id in it you need correct?

    With regards to your $has_many property, is your prizes_model in the fuel/modules/fuel/models folder? If not, the FUEL_FOLDER array key should be changed to the module it belongs to with "app" being used for the application directory.

    With regards to the on_after_save, you need to call parent::on_after_save($values); from within the on_after_save method like so:
    function on_after_save($values) { $values = parent::on_after_save($values); .... return $values; }
  • edited 7:46PM
    Ok. I added the $values = parent::on_after_save($values); line to my on_after_save method and that got me where I needed to be. Got the 2 to play nice with each other. Both are saving now. Thanks a lot for the help. If there were any way to get variables into has_many it would be the bomb!
Sign In or Register to comment.