1
votes

*EDIT/UPDATE*

I found a solution but don't really know why this has to be. If anyone could answer why this is that would be great.

I found that the MediaFloorplan would show up in my data when I added "floorplan_id" to my field list for MoveInHome being that MoveInHome hasMany Floorplan. Strange because "id" is found within the fields for the contained "Floorplan"

*END EDIT/UPDATE*

I'm working on a project for a home builder and am having a problem getting some data via containable.

App model has this:

public $actsAs = array("Containable");

I'm making a call from FloorplansController to MoveInHome model

$this->loadModel("MoveInHome");
$moveInReadyData = $this->MoveInHome->getByCommunity($communityid,4);

MoveInHome belongsTo Floorplan

Floorplan hasMany MediaFloorplan

Here's the function in the model:

public function getByCommunity($id,$limit=4) {

        $params = array(
            "fields" => array(
                "address",
                "city",
                "state",
            ),
            "conditions" => array(
                "MoveInHome.community_id" => $id,
                "MoveInHome.active" => 1,
                "MoveInHome.sold" => 0
            ),
            "limit" => $limit,
            "order" => "RAND()",
            "contain" => array(
                "MediaMoveInHome" => array(
                    "order" => array(
                        "featured" => "DESC"
                    ),
                    "fields" => array(
                        "media_move_in_home_file_path",
                    ),
                    "limit" => 1,
                ),
                "Floorplan" => array(
                    "fields" => array(
                        "id",
                        "name",
                        "style",
                        "bedrooms",
                        "bathrooms",
                        "sf_bsmt",
                        "sf_1flr",
                        "sf_2flr"
                    ),
                    "MediaFloorplan" => array(
                        "conditions" => array(
                            "MediaFloorplan.type" => "main",
                        ),
                        "fields" => array(
                                "id",
                                "media_floorplan_file_path"
                            )
                    )

                )

            )

        );

        return $this->find("all", $params);

    }

All of the data comes back fine except for that MediaFloorplan doesn't come back at all. Note that I've made a similar contain request in other areas of the site. Would the problem stem from making the request from the FloorplansController to MoveInReady and trying to contain Floorplan, etc?

Thanks for the help.

1
Have you tried looking at the SQL that Cake is generating for both your working and non-working versions? That will probably provide you with some insight. Do <?php //echo $this->element('sql_dump'); ?> in your view, or use the awesome Debug Kit plugin - github.com/cakephp/debug_kitjoshua.paling
You edit is correct. Containable joins data based on the primary and foreign keys defined in your model relationships. If you do not select all the related id's in your find, the behaviour will not know how to join the data together. If in doubt, always pull out all the ids.David Yell

1 Answers

0
votes

As you found yourself, and DavidYell clarified in his comment on the question, you have to include the foreign keys in the fields option. From the actual CakePHP documentation: here

When using ‘fields’ and ‘contain’ options - be careful to include all foreign keys that your query directly or indirectly requires. Please also note that because Containable must to be attached to all models used in containment, you may consider attaching it to your AppModel.