Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthorcralls
     

    Hi All,

    I'm new to Akelos and OOP so please forgive me if I'm missing something obvious here.

    I have a user model that has_many user_services. (user_services has id, user_id, and service_id columns).

    In the user/add view I am creating checkboxes for each service:

        <?php $services = $Service->collect($Service->find(), 'id', 'service');?>
        <?php $i = 1; ?>
        <?php foreach($services as $service) { ?>
        <?php   echo $form_tag_helper->check_box_tag('user[user_service]', $i)." $service<br/>\n"; ?>
        <?php   $i++; ?>
        <?php } ?>
    

    How do I save each of the services checked in my user_controller to the user_services table? I thought $this->User->save() would save each service to the user_services table for me? Perhaps I'm not formatting the checkboxes properly?

    Thanks much and best regards,

    ~Casey

    •  
      CommentAuthorbermi
     

    Hi Casey, and welcome to the forum.

    If I where you I would use the service model id instead of an incremental $i :)

    You can do something like:

    <?php $services = $Service->collect($Service->find(), 'id', 'service');?>
    {loop services}
        <%=checkbox_tag 'services[]', service_loop_key %> {service}
    
    {end}
    

    I have not tested this, but you should have a list of services in your controller at $this->params['services']

    Good luck

    • CommentAuthorcralls
     

    Hi Bermi,

    It's a pleasure to make your acquaintance. I admire your hard work on this framework and thank you for the loop code. I like that much better then having a foreach statement in my view.

    I was able to get this working. I'm just not sure if I'm saving $this->params['user_services'] using "best practices". This is what I have in my controller for the add function:

    function add()
    {
        if(!empty($this->params['user'])){
            $this->User->setAttributes($this->params['user']);
            foreach($this->params['user_services'] as $service) {
                $this->User->user_service->create(array('user_id' => $this->User->getId(), 'service_id' => $service));
            }
            if ($this->Request->isPost() && $this->User->save()){
                $this->flash['notice'] = $this->t('User was successfully created.');
                $this->redirectTo(array('action' => 'show', 'id' => $this->User->getId()));
            }
        }
    }
    

    I've added the foreach loop in order to save to the user_services table but I'm not sure I like it. I was hoping that if I named the checkbox fields properly in the view, for example: user_user_services[] or user[user_services][], that $this->User->save() would automatically write those records to the user_services table?

    Best Regards and Thanks again,

    ~Casey

    •  
      CommentAuthorbermi
     

    Best practices would recommend to use a has_and_belongs_to_many association between User and Service like

    class User extends ActiveRecord{
      var $habtm = 'services';
    }
    
    class Service extends ActiveRecord{
      var $habtm = 'users';
    }
    

    Then in your controller you could do

    $User->service->setByIds($this->params['user_services']);
    

    Loading user services in your listing action could be done like

    $User->find('all', array('include'=>'services'));