0

我正在尝试编写一个类,以允许您在基于 php MVC 的项目中建立两个模型之间的关系。我想创建一个只有你想要的字段的自定义对象所以我写了这个:

class CreateRelationShipObjects {

    private $modelObjects;

    public function __construct($modelObjects){
        if(is_array($modelObjects) && isset($modelObjects)){
            $this->modelObjects = $modelObjects;
        }
    }

    public function createRelationShip($columns){
        if(is_array($columns)){
           foreach($columns as $custom_name=>$column){
               foreach($this->modelObjects as $name => $modelObject){
                   $this->checkForColumnInModelObject($modelObject, $column, $custom_name);
               }
           } 
        }
    }

    private function checkForColumnInModelObject($modelObject, $column, $custom_name){
        $relationship = array();
        if(property_exists($modelObject, $column)){
            $value_returned = $modelObject->$column;
            $relationship = array($column => $value_returned);
            var_dump($relationship);
        }
        //return $this->toObj($relationship);
    }

    private function toObj($releationshipArray){
       //var_dump((Object)$releationshipArray);
    }
}

这个想法是你使用这个类:

 $relationship = new CreateRelationShipObjects(array(
     'equipment_ip_models' => $equipmentIpModels,
     'site_models' => $siteModel,
     'equipment_models' => $equipmentModel
 ));

 $relationship->createRelationShip(array(
     'equipment_model' => 'model',
     'equipment_manufacture' => 'manufacture',
     'equipment_ip' => 'ipAddress',
     'site_name' => 'name',
     'site_id' => 'code'
 ));

在上面的课程中,我们将查看为该列提供的每个模型。然后给你一个看起来与你提供的“模板”一模一样的对象createRelationShip

问题是,我回来了,我var_dump的 , 像这样的数组:

array(1) {
  ["model"]=>
  string(13) "4168.21.33.03"
}

array(1) {
  ["manufacture"]=>
  string(6) "ALLGON"
}

// ... And so on

数组应如下所示:

array(
    'equipment_model' => '4168.21.33.03',
    'equipment_manufacture' => 'ALLGON',
    // .. and so on ... 
); 

有人可以告诉我我做错了什么吗?

4

2 回答 2

0

也许这效果更好?如果修改这两个函数呢?

public function createRelationShip($columns){
    if(is_array($columns)){
       $relationship = array();
       foreach($columns as $custom_name=>$column){
           foreach($this->modelObjects as $name => $modelObject){
               $relationship += $this->checkForColumnInModelObject($modelObject, $column, $custom_name);
           }
       } 
       var_dump($relationship);
    }
}

private function checkForColumnInModelObject($modelObject, $column, $custom_name){
    if(property_exists($modelObject, $column)){
        $value_returned = $modelObject->$column;
        return array($column => $value_returned);
    }
    return array();
}
于 2013-10-02T13:32:21.623 回答
0

下面的课程基本上是我需要做的。我花了整个晚上,但我写了它。它接收我传递给它的东西,然后给我一个我想要的东西。

class RelationshipObject {
  /**
   * Stores the models
   * 
   * @var type 
   */  
  private $modelObjects;

  /**
   * Takes in the objects
   * 
   * @param array $objects
   */
  public function __construct($objects) {
    $this->modelObjects = $objects;
  }

  /**
   * Creates a custom object based on the array passed in.
   * 
   * @param array $outputKeysToInputKeys
   * @return obj
   */
  public function create(array $outputKeysToInputKeys) {
    $result = array();
    foreach ($outputKeysToInputKeys as $outputKey => $inputKey) {
      foreach ($this->modelObjects as $object) {
        if (property_exists($object, $inputKey))
          $result[$outputKey] = $object->$inputKey;
      }
    }

    return $this->to_obj($result);
  }

  /**
   * Converts an array to an object.
   * 
   * @param array $array
   * @return obj
   */
  public function to_obj($array){
      return (Object)$array;
  }
}
于 2013-10-02T17:38:36.363 回答