Note that there are some explanatory texts on larger screens.

plurals
  1. POPutting the array back together
    primarykey
    data
    text
    <p>I am trying to write a class to allow you to set up a relationship between two models in a php MVC based project. I want to create a custom object that has only the fields you want in it So I wrote this:</p> <pre><code>class CreateRelationShipObjects { private $modelObjects; public function __construct($modelObjects){ if(is_array($modelObjects) &amp;&amp; isset($modelObjects)){ $this-&gt;modelObjects = $modelObjects; } } public function createRelationShip($columns){ if(is_array($columns)){ foreach($columns as $custom_name=&gt;$column){ foreach($this-&gt;modelObjects as $name =&gt; $modelObject){ $this-&gt;checkForColumnInModelObject($modelObject, $column, $custom_name); } } } } private function checkForColumnInModelObject($modelObject, $column, $custom_name){ $relationship = array(); if(property_exists($modelObject, $column)){ $value_returned = $modelObject-&gt;$column; $relationship = array($column =&gt; $value_returned); var_dump($relationship); } //return $this-&gt;toObj($relationship); } private function toObj($releationshipArray){ //var_dump((Object)$releationshipArray); } } </code></pre> <p>The idea is that you use the class as such:</p> <pre><code> $relationship = new CreateRelationShipObjects(array( 'equipment_ip_models' =&gt; $equipmentIpModels, 'site_models' =&gt; $siteModel, 'equipment_models' =&gt; $equipmentModel )); $relationship-&gt;createRelationShip(array( 'equipment_model' =&gt; 'model', 'equipment_manufacture' =&gt; 'manufacture', 'equipment_ip' =&gt; 'ipAddress', 'site_name' =&gt; 'name', 'site_id' =&gt; 'code' )); </code></pre> <p>In the class above we will go through looking in each model supplied for the column. and then give you back an object that looks exactly like the "template" you supplied in <code>createRelationShip</code></p> <p>The problem is, I am getting back, with my <code>var_dump</code>, an arrays like this:</p> <pre><code>array(1) { ["model"]=&gt; string(13) "4168.21.33.03" } array(1) { ["manufacture"]=&gt; string(6) "ALLGON" } // ... And so on </code></pre> <p>The array should look like:</p> <pre><code>array( 'equipment_model' =&gt; '4168.21.33.03', 'equipment_manufacture' =&gt; 'ALLGON', // .. and so on ... ); </code></pre> <p><strong>Can some one tell me what I am doing wrong?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload