Saving and Reading Many-to-Many Relationships in Yii

Finally after a half year of trial and error, I was able to resolve the problem of saving and reading many-to-many relationships in Yii with help from the following two links:
http://www.tipstank.com/2010/07/19/retrieving-selected-checkbox-items-in-yii/ (for the reading)
http://www.yiiframework.com/forum/index.php?/topic/16936-many-to-many/ (for the saving)

In my project, a Place can have many Types and a Type can have many Places. Creation, reading, and updating is done by way of multi-selectable checkboxes.

In Place controller:

if(isset($_POST['Place']))
{
    $model->attributes=$_POST['Place'];
    $model->typeIds = Type::model()->findAll();
    if($model->save())
        $this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
    'model'=>$model,
));

In the Place model (active record):

public $typeIds = array();
 
...
public function relations()
{
    return array(
        'types' => array(self::MANY_MANY, 'Type', 'place_type(place_id, type_id)','index'=>'id'),
    );
}
 
public function behaviors() 
{
    return array('CAdvancedArBehavior' => array(
        'class' => 'application.extensions.CAdvancedArBehavior'
    ));
}
 
public function afterFind()
{
    $this->typeIds = array_keys($this->types);
 
    if (!empty($this->_types))
    {
        foreach ($this->_types as $n => $type)
            $this->typeIds[] = $type->id;
    }
    parent::afterFind();
}

In Place view _form template:

<div class="row">
<?php echo $form->labelEx($model,'type'); ?>
<?php echo $form->checkBoxList($model,'typeIds', 
    CHtml::listData(Type::model()->findAll(), 'id', 'name')); ?>
<?php echo $form->error($model, 'type'); ?>
</div>
Posted at 8pm on 1/9/12 | no comments; | Filed Under: Webdev | Tagged: | read on

Giovanni Hidalgo on the timbales


I love the energy of the first impromptu performance.

Posted at 11pm on 11/3/11 | no comments; | Filed Under: Drums & Percussion | Tagged: , | read on

Tito Puente: The High Priest of Salsa Music


The video that caused me to order a set of timbales.

Posted at 5am on 10/21/11 | no comments; | Filed Under: Drums & Percussion | Tagged: , | read on

“Music is expression of harmony in sound. Love is the expression of harmony in life.”

Posted at 8am on 9/30/11 | no comments; | Filed Under: Quotes | read on

More from Mr. Ducroz


I love this guys’s work.

Posted at 9pm on 7/20/11 | no comments; | Filed Under: Motion Graphics | read on

Stop motion + CG printed on paper = totally awesome.

In my opinion, these motion graphic pieces are the most inspiring I’ve seen since Michel Levy’s Giant Steps video. By Benjamin Ducroz.

Posted at 4am on 7/9/11 | no comments; | Filed Under: Motion Graphics | read on

LABjs

It was eating away at me everyday at work that we didn’t have a system in place to manage dependencies. Besides efficiently handling dependencies, it is something we need to learn about and what better way to do that other than integrating such a system in our own product? So we tried RequireJS and realized that it was not well-suited for our current codebase. In order to really take advantage of RequireJS, it is recommended that you use their module defining method to build blocks of protected code. It might have been great had we started building our app that way, but I felt it would be too huge an undertaking to overhaul our code to that extent with no guarantee that it will be the best solution. So we opted for LABjs, another open source script loading managing system. I’ll write an update at the end of this sprint when it will have been fully integrated into our site.

Posted at 11am on 6/25/11 | no comments; | Filed Under: Webdev | Tagged: , | read on

“The question isn’t who is going to let me; it’s who is going to stop me.”

— Ayn Rand

Posted at 2am on 6/17/11 | no comments; | Filed Under: Quotes | read on

Thinking Space: A MindMapping App for Android

I’ve been into mindmapping ever since I read Paul Scheele’s PhotoReading. While on an Android app downloading frenzy, I came across Thinking Space and decided to give it a try. Although it lacks a much-needed web-based component that would enable you to view your mindmaps online without downloading them, they at least have the next best thing: a way to save your mindmaps via Google’s cloud service. You can in turn download/sync them to another Android device and assuming Thinking Space is also installed there, you will be able to open them without a hitch. So now I can work on my mindmaps on either my nook or my Droid. With the increasing trend toward this niche app market, I’m sure they’ll come up with a way to do the online version. Those who know me well know that I get a little obsessive about my nested lists. Hence Thinking Space is a haven for me; Mindmapping is a nonlinear organic approach to nested listmaking. Check it out: thinkingspace.net.

Posted at 1am on 5/10/11 | no comments; | Filed Under: Android | Tagged: | read on

Yii Project Round 3

Yes, it’s my third crack at this. The second round was ok. I moved the project to a new computer and there was a problem that prevented me from moving forward for a month or so, the solution to which was a missing unix_socket to my MAMP mysql.sock, “/Applications/MAMP/tmp/mysql/mysql.sock”. But I am determined to once and for all get it this time. I started my Places website once again alongside my Yii project. So far, I’ve managed to create Active Record models for place, type, and place_type. The place_type many-to-many relationship was a hairy problem I encountered in my first iteration of the Places website. I was able to figure out how to make it work, but making it work within the Yii framework might prove to be very difficult. We will see.

Posted at 5am on 5/9/11 | no comments; | Filed Under: Webdev | read on

First entry from my nookCOLOR

This is not only my first nook entry, but also my first entry from a native WordPress phone app. Ever since I got my Droid (first smartphone), I wanted to use the native WordPress app for Android but couldn’t because of some error. Well, I’ve decided to tackle it this time since I bought yet another device to blog with on the go.

The fix, which I found via googling, was very simple. In the .htaccess file of my site’s root directory, I just added…

<IfModule mod_security.c>
    SecFilterScanPOST Off
</IfModule>
Posted at 11am on 4/30/11 | no comments; | Filed Under: Wordpress | read on