Пример #1
0
 /**
  * Magic Getter. Gets the value for a specific property in the bean.
  * If the property does not exist this getter will make sure no error
  * occurs. This is because RedBean allows you to query (probe) for
  * properties. If the property can not be found this method will
  * return NULL instead.
  * @param string $property
  * @return mixed $value
  */
 public function &__get($property)
 {
     if ($this->beanHelper) {
         $toolbox = $this->beanHelper->getToolbox();
     }
     if (!isset($this->properties[$property])) {
         $fieldLink = $property . "_id";
         /**
          * All this magic can be become very complex quicly. For instance,
          * my PHP CLI produced a segfault while testing this code. Turns out that
          * if fieldlink equals idfield, scripts tend to recusrively load beans and
          * instead of giving a clue they simply crash and burn isnt that nice?
          */
         if (isset($this->{$fieldLink}) && $fieldLink != $this->getMeta('sys.idfield')) {
             $this->setMeta("tainted", true);
             $type = $toolbox->getWriter()->getAlias($property);
             $targetType = $this->properties[$fieldLink];
             $bean = $toolbox->getRedBean()->load($type, $targetType);
             //return $bean;
             $this->properties[$property] = $bean;
             return $this->properties[$property];
         }
         if (strpos($property, 'own') === 0) {
             $firstCharCode = ord(substr($property, 3, 1));
             if ($firstCharCode >= 65 && $firstCharCode <= 90) {
                 $type = lcfirst(str_replace('own', '', $property));
                 $myFieldLink = $this->getMeta('type') . "_id";
                 $beans = $toolbox->getRedBean()->find($type, array(), array(" {$myFieldLink} = ? ", array($this->getID())));
                 $this->properties[$property] = $beans;
                 $this->setMeta("sys.shadow." . $property, $beans);
                 $this->setMeta("tainted", true);
                 return $this->properties[$property];
             }
         }
         if (strpos($property, 'shared') === 0) {
             $firstCharCode = ord(substr($property, 6, 1));
             if ($firstCharCode >= 65 && $firstCharCode <= 90) {
                 $type = lcfirst(str_replace('shared', '', $property));
                 $keys = $toolbox->getRedBean()->getAssociationManager()->related($this, $type);
                 if (!count($keys)) {
                     $beans = array();
                 } else {
                     $beans = $toolbox->getRedBean()->batch($type, $keys);
                 }
                 $this->properties[$property] = $beans;
                 $this->setMeta("sys.shadow." . $property, $beans);
                 $this->setMeta("tainted", true);
                 return $this->properties[$property];
             }
         }
         return $this->null;
     }
     return $this->properties[$property];
 }
Пример #2
0
 /**
  * Counts all shared beans of type $type.
  * Also works with via(), with() and withCondition().
  *
  * @param string $type type of bean you wish to count
  *
  * @return integer
  */
 public function countShared($type)
 {
     $toolbox = $this->beanHelper->getToolbox();
     $redbean = $toolbox->getRedBean();
     $writer = $toolbox->getWriter();
     if ($this->via) {
         $oldName = $writer->getAssocTable(array($this->__info['type'], $type));
         if ($oldName !== $this->via) {
             //set the new renaming rule
             $writer->renameAssocTable($oldName, $this->via);
             $this->via = NULL;
         }
     }
     $type = $this->beau($type);
     $count = 0;
     if ($this->getID() > 0) {
         $count = $redbean->getAssociationManager()->relatedCount($this, $type, $this->withSql, $this->withParams, TRUE);
     }
     $this->withSql = '';
     $this->withParams = array();
     return (int) $count;
 }
Пример #3
0
 /**
  * Reroutes a call to Model if exists. (new fuse)
  * @param string $method
  * @param array $args
  * @return mixed $mixed
  */
 public function __call($method, $args)
 {
     return null;
     if (!isset($this->__info['model'])) {
         $model = $this->beanHelper->getModelForBean($this);
         if (!$model) {
             return;
         }
         $this->__info['model'] = $model;
     }
     if (!method_exists($this->__info['model'], $method)) {
         return null;
     }
     return call_user_func_array(array($this->__info['model'], $method), $args);
 }
Пример #4
0
 /**
  * Creates a N-M relation by linking an intermediate bean.
  * This method can be used to quickly connect beans using indirect
  * relations. For instance, given an album and a song you can connect the two
  * using a track with a number like this:
  * 
  * Usage:
  * 
  * $album->link('track', array('number'=>1))->song = $song;
  * 
  * or:
  * 
  * $album->link($trackBean)->song = $song;
  * 	
  * What this method does is adding the link bean to the own-list, in this case
  * ownTrack. If the first argument is a string and the second is an array or
  * a JSON string then the linking bean gets dispensed on-the-fly as seen in
  * example #1. After preparing the linking bean, the bean is returned thus
  * allowing the chained setter: ->song = $song.
  * 
  * @param string|RedBean_OODBBean $type          type of bean to dispense or the full bean
  * @param string|array            $qualification JSON string or array (optional)
  */
 public function link($typeOrBean, $qualification = array())
 {
     if (is_string($typeOrBean)) {
         $bean = $this->beanHelper->getToolBox()->getRedBean()->dispense($typeOrBean);
         if (is_string($qualification)) {
             $data = json_decode($qualification, true);
         } else {
             $data = $qualification;
         }
         foreach ($data as $key => $value) {
             $bean->{$key} = $value;
         }
     } else {
         $bean = $typeOrBean;
     }
     $list = 'own' . ucfirst($bean->getMeta('type'));
     array_push($this->{$list}, $bean);
     return $bean;
 }