RedBeanPHP\AssociationManager::associateBeans PHP Method

associateBeans() protected method

This method is used by associate. This method also accepts a base bean to be used as the template for the link record in the database.
protected associateBeans ( redbeanphp\OODBBean $bean1, redbeanphp\OODBBean $bean2, redbeanphp\OODBBean $bean ) : mixed
$bean1 redbeanphp\OODBBean first bean
$bean2 redbeanphp\OODBBean second bean
$bean redbeanphp\OODBBean base bean (association record)
return mixed
    protected function associateBeans(OODBBean $bean1, OODBBean $bean2, OODBBean $bean)
    {
        $type = $bean->getMeta('type');
        $property1 = $bean1->getMeta('type') . '_id';
        $property2 = $bean2->getMeta('type') . '_id';
        if ($property1 == $property2) {
            $property2 = $bean2->getMeta('type') . '2_id';
        }
        $this->oodb->store($bean1);
        $this->oodb->store($bean2);
        $bean->setMeta("cast.{$property1}", "id");
        $bean->setMeta("cast.{$property2}", "id");
        $bean->setMeta('sys.buildcommand.unique', array($property1, $property2));
        $bean->{$property1} = $bean1->id;
        $bean->{$property2} = $bean2->id;
        $results = array();
        try {
            $id = $this->oodb->store($bean);
            $results[] = $id;
        } catch (SQLException $exception) {
            if (!$this->writer->sqlStateIn($exception->getSQLState(), array(QueryWriter::C_SQLSTATE_INTEGRITY_CONSTRAINT_VIOLATION))) {
                throw $exception;
            }
        }
        return $results;
    }