Exemplo n.º 1
0
 /**
  * Constructs a new RedBeanModels which is a collection of classes extending model.
  * The models are created lazily.
  * Models are only constructed with beans by the model. Beans are
  * never used by the application directly.
  * The application can construct a new models object by constructing
  * them without specifying a bean. In other words, if Php had overloading
  * and friends the constructor without the $bean would be public, and the
  * constructor taking a bean would private and available only to RedBeanModel.
  */
 public function __construct($modelClassName, $sqlOrBean = '')
 {
     assert('is_string($sqlOrBean) || $sqlOrBean instanceof RedBean_OODBBean');
     $this->modelClassName = $modelClassName;
     $this->position = 0;
     $tableName = RedBeanModel::getTableName($modelClassName);
     if (is_string($sqlOrBean)) {
         $this->relatedBeansAndModels = array_values(R::find($tableName, $sqlOrBean));
     } else {
         assert('$sqlOrBean instanceof RedBean_OODBBean');
         $this->bean = $sqlOrBean;
         // I had this...
         // $this->relatedBeansAndModels = array_values(R::related($this->bean, $tableName));
         // and the doco says to use related in place of findLinks which is deprecated,
         // but that is returning zero when I know there are linked beans.
         try {
             // So I'm getting them with the link manager, but that can
             // throw if the table or column doesn't exist yet because there
             // are no linked beans.
             if ($this->bean->id > 0) {
                 $relatedIds = ZurmoRedBeanLinkManager::getKeys($this->bean, $tableName);
                 $this->relatedBeansAndModels = array_values(R::batch($tableName, $relatedIds));
             } else {
                 $this->relatedBeansAndModels = array();
             }
         } catch (RedBean_Exception_SQL $e) {
             // SQLSTATE[42S02]: Base table or view not found...
             // SQLSTATE[42S22]: Column not found...
             if (!in_array($e->getSQLState(), array('42S02', '42S22'))) {
                 throw $e;
             }
             // If there is nothing yet linked
             // just have no related models yet.
             $this->relatedBeansAndModels = array();
         }
     }
 }
 public function save($runValidation = true)
 {
     if (!parent::save($runValidation)) {
         return false;
     }
     foreach ($this->deferredRelateBeans as $bean) {
         if ($this->linkType == RedBeanModel::LINK_TYPE_POLYMORPHIC) {
             if ($this->bean->id == null) {
                 ZurmoRedBean::store($this->bean);
             }
             $polyIdFieldName = strtolower($this->linkName) . '_id';
             $polyTypeFieldName = strtolower($this->linkName) . '_type';
             $bean->{$polyTypeFieldName} = $this->bean->getMeta('type');
             $bean->{$polyIdFieldName} = $this->bean->id;
         } else {
             ZurmoRedBeanLinkManager::link($bean, $this->bean, $this->resolveLinkNameForCasing());
         }
         ZurmoRedBean::store($bean);
     }
     $this->deferredRelateBeans = array();
     $relatedModelClassName = $this->relatedModelClassName;
     $tableName = $relatedModelClassName::getTableName();
     foreach ($this->deferredUnrelateBeans as $bean) {
         if (!$this->owns) {
             if ($this->linkType == RedBeanModel::LINK_TYPE_POLYMORPHIC) {
                 throw new NotSupportedException("Polymorphic relations can not be NOT_OWNED");
             }
             ZurmoRedBeanLinkManager::breakLink($bean, $tableName, $this->resolveLinkNameForCasing());
             ZurmoRedBean::store($bean);
         } else {
             ZurmoRedBean::trash($bean);
         }
     }
     $this->deferredUnrelatedBeans = array();
     foreach ($this->deferredUnrelatedModels as $model) {
         $event = new CModelEvent($model);
         $model->onRedBeanOneToManyRelatedModelsChange($event);
     }
     $this->deferredUnrelatedModels = array();
     return true;
 }
Exemplo n.º 3
0
 protected function linkBeans()
 {
     $baseModelClassName = null;
     $baseBean = null;
     foreach ($this->modelClassNameToBean as $modelClassName => $bean) {
         if ($baseBean !== null) {
             ZurmoRedBeanLinkManager::link($bean, $baseBean);
             if (!RedBeanDatabase::isFrozen()) {
                 $tableName = self::getTableName($modelClassName);
                 $columnName = self::getTableName($baseModelClassName) . '_id';
                 RedBeanColumnTypeOptimizer::optimize($tableName, $columnName, 'id');
             }
         }
         $baseModelClassName = $modelClassName;
         $baseBean = $bean;
     }
 }
 public function save($runValidation = true)
 {
     if (!parent::save($runValidation)) {
         return false;
     }
     foreach ($this->deferredRelateBeans as $bean) {
         if ($this->linkType == RedBeanModel::LINK_TYPE_POLYMORPHIC) {
             if ($this->bean->id == null) {
                 R::store($this->bean);
             }
             $polyIdFieldName = strtolower($this->linkName) . '_id';
             $polyTypeFieldName = strtolower($this->linkName) . '_type';
             $bean->{$polyTypeFieldName} = $this->bean->getMeta('type');
             $bean->{$polyIdFieldName} = $this->bean->id;
             if (!RedBeanDatabase::isFrozen()) {
                 $tableName = RedBeanModel::getTableName($this->modelClassName);
                 RedBeanColumnTypeOptimizer::optimize($tableName, $polyIdFieldName, 'id');
             }
         } else {
             ZurmoRedBeanLinkManager::link($bean, $this->bean, $this->resolveLinkNameForCasing());
             if (!RedBeanDatabase::isFrozen()) {
                 $tableName = RedBeanModel::getTableName($this->modelClassName);
                 $columnName = RedBeanModel::getTableName($this->relatedModelClassName) . '_id';
                 $columnName = ZurmoRedBeanLinkManager::resolveColumnPrefix($this->resolveLinkNameForCasing()) . $columnName;
                 RedBeanColumnTypeOptimizer::optimize($tableName, $columnName, 'id');
             }
         }
         R::store($bean);
     }
     $this->deferredRelateBeans = array();
     $tableName = RedBeanModel::getTableName($this->relatedModelClassName);
     foreach ($this->deferredUnrelateBeans as $bean) {
         if (!$this->owns) {
             ZurmoRedBeanLinkManager::breakLink($bean, $tableName, $this->resolveLinkNameForCasing());
             R::store($bean);
         } else {
             R::trash($bean);
         }
     }
     $this->deferredUnrelateBeans = array();
     return true;
 }
Exemplo n.º 5
0
 protected function linkBeans()
 {
     // Link the beans up the inheritance hierarchy, skipping
     // the person bean, then link that to the user. So the
     // user is linked to both the person and the permitable,
     // to complete the mixing in of the Person's data.
     $baseBean = null;
     foreach ($this->modelClassNameToBean as $modelClassName => $bean) {
         if ($modelClassName == 'Person') {
             continue;
         }
         if ($baseBean !== null) {
             ZurmoRedBeanLinkManager::link($bean, $baseBean);
         }
         $baseBean = $bean;
     }
     $userBean = $this->modelClassNameToBean['User'];
     $personBean = $this->modelClassNameToBean['Person'];
     ZurmoRedBeanLinkManager::link($userBean, $personBean);
 }
Exemplo n.º 6
0
 protected function linkBeans()
 {
     // Link the beans up the inheritance hierarchy, skipping
     // the person bean, then link that to the user. So the
     // user is linked to both the person and the permitable,
     // to complete the mixing in of the Person's data.
     $baseBean = null;
     foreach ($this->modelClassNameToBean as $modelClassName => $bean) {
         if ($modelClassName == 'Person') {
             continue;
         }
         if ($baseBean !== null) {
             ZurmoRedBeanLinkManager::link($bean, $baseBean);
             if (!RedBeanDatabase::isFrozen()) {
                 $tableName = self::getTableName(get_class($this));
                 $columnName = 'person_id';
                 RedBeanColumnTypeOptimizer::optimize($tableName, $columnName, 'id');
             }
         }
         $baseBean = $bean;
     }
     $userBean = $this->modelClassNameToBean['User'];
     $personBean = $this->modelClassNameToBean['Person'];
     ZurmoRedBeanLinkManager::link($userBean, $personBean);
     if (!RedBeanDatabase::isFrozen()) {
         $tableName = self::getTableName(get_class($this));
         RedBeanColumnTypeOptimizer::optimize($tableName, 'person_id', 'id');
     }
 }
Exemplo n.º 7
0
 protected function linkBeans()
 {
     $baseBean = null;
     foreach ($this->modelClassNameToBean as $modelClassName => $bean) {
         if ($baseBean !== null) {
             ZurmoRedBeanLinkManager::link($bean, $baseBean);
         }
         $baseBean = $bean;
     }
 }
Exemplo n.º 8
0
 public function testCascadedDeleteDoesNotWorkForLinkedBeans()
 {
     $member = ZurmoRedBean::dispense('marketinglistmember');
     $member->unsubscribed = true;
     ZurmoRedBean::store($member);
     $list = ZurmoRedBean::dispense('marketinglist');
     $list->name = 'dummy';
     ZurmoRedBean::store($list);
     ZurmoRedBeanLinkManager::link($member, $list);
     ZurmoRedBean::store($member);
     $id = $list->id;
     unset($list);
     ZurmoRedBean::trash($member);
     unset($member);
     $list = ZurmoRedBean::load('marketinglist', $id);
     $this->assertNotNull($list);
     // The list is not deleted.
 }
Exemplo n.º 9
0
 public function testCascadedDeleteDoesNotWorkForLinkedBeans()
 {
     $person = R::dispense('person');
     $person->name = 'bill';
     R::store($person);
     $phone = R::dispense('phone');
     $phone->number = '555-1234';
     R::store($phone);
     ZurmoRedBeanLinkManager::link($phone, $person);
     R::store($phone);
     $id = $phone->id;
     unset($phone);
     R::trash($person);
     unset($person);
     $phone = R::load('phone', $id);
     $this->assertNotNull($phone);
     // The phone is not deleted.
 }