コード例 #1
0
ファイル: oodb.php プロジェクト: Jamongkad/Sunfish
 /**
  * To perform a command normally handles by the magic method __call
  * use this one. This makes it easy to overwrite a method like set
  * and then route its result to the original method
  * @param $method
  * @param $arguments
  * @return unknown_type
  */
 public function command($method, $arguments)
 {
     if (strpos($method, "set") === 0) {
         $prop = substr($method, 3);
         $this->{$prop} = $arguments[0];
         return false;
     } elseif (strpos($method, "oset") === 0) {
         $prop = strtolower(substr($method, 4));
         $obj = $arguments[0];
         if (!is_null($obj)) {
             $id = intval($obj->getID());
             //id must not be 0!
             if (!$id) {
                 $obj->save();
                 $id = intval($obj->getID());
                 if (!$id) {
                     return false;
                 }
             }
         } else {
             $id = 0;
         }
         $this->data->{$prop} = $id;
         return false;
     } elseif (strpos($method, "getRelated") === 0) {
         $prop = strtolower(substr($method, 10));
         $beans = RedBean_OODB::getAssoc($this->data, $prop);
         $decos = array();
         $dclass = PRFX . $prop . SFFX;
         if ($beans && is_array($beans)) {
             foreach ($beans as $b) {
                 $d = new $dclass();
                 $d->setData($b);
                 $decos[] = $d;
             }
         }
         return $decos;
     } elseif (strpos($method, "get") === 0) {
         $prop = substr($method, 3);
         return $this->{$prop};
     } elseif (strpos($method, "oget") === 0) {
         $prop = strtolower(substr($method, 4));
         $id = intval($this->data->{$prop});
         $classname = PRFX . $prop . SFFX;
         $obj = new $classname($id);
         return $obj;
     } elseif (strpos($method, "is") === 0) {
         $prop = strtolower(substr($method, 2));
         return $this->data->{$prop} ? TRUE : FALSE;
     } else {
         if (strpos($method, "add") === 0) {
             //@add
             $deco = $arguments[0];
             $bean = $deco->getData();
             RedBean_OODB::associate($this->data, $bean);
             return $this;
         } else {
             if (strpos($method, "remove") === 0) {
                 $deco = $arguments[0];
                 $bean = $deco->getData();
                 RedBean_OODB::unassociate($this->data, $bean);
                 return $this;
             } else {
                 if (strpos($method, "attach") === 0) {
                     $deco = $arguments[0];
                     $bean = $deco->getData();
                     RedBean_OODB::addChild($this->data, $bean);
                     return $this;
                 } else {
                     if (strpos($method, "clearRelated") === 0) {
                         $type = strtolower(substr($method, 12));
                         RedBean_OODB::deleteAllAssocType($type, $this->data);
                         return $this;
                     } else {
                         if (strpos($method, "numof") === 0) {
                             $type = strtolower(substr($method, 5));
                             return RedBean_OODB::numOfRelated($type, $this->data);
                         }
                     }
                 }
             }
         }
     }
 }