/** * @dataProvider tabeleTrue */ public function testSave($tabel, $data) { $table = new table($tabel); foreach ($data as $key => $value) { $table->{$key} = $value; } $table->save(); $table = table::findS($tabel, $data); $this->assertNotNull($table); $pK = $table->getPrimaryKey(TRUE); return $pK; }
<?php include '../rolisz.php'; rolisz::connect('MySQLi', 'localhost', 'root', '', 'rolisz'); include '../db.php'; table::set('posts'); table::addRelationM2MS('posts', 'post_category', 'id', 'pid', 'category', 'id', 'cid'); table::addRelationM2MS('posts', 'post_tags', 'id', 'pid', 'tags', 'id', 'tid'); table::addRelationS('posts', 'users', 'author', 'id'); table::addRelationS('posts', 'comments', 'pid'); //var_dump(table::findS('posts')); $posts = new table('posts'); // $posts->addRelationM2M('post_category','id','pid','category','id','cid'); // $posts->addRelation('users','author','id'); // $posts->addRelation('comments','pid'); $posts->title = 'Test Post'; $posts->body = 'Lorem Ipsum Doloret coloret'; $posts->author = 1; //$posts->save(); $posts1 = new table('posts', 1); $test = $posts1->find('category'); $comment = new table('comments', 1); $comment->posts(array('id' => 1)); $comment->save(); $author = new table('users', 1); $posts2 = new table('posts', 2); $posts2->tags(array('tag' => 'google'));
/** * Edit the ACL. Parameters the same as for addNew(), except an extra parameter. * @param string $type It can be 'resource', 'action', 'requester' or 'permission' * @param mixed $what For 'resource', 'action' and 'requester' it should be a string containing the name of the new * element. For 'permission' it should be an associative array containing key-value pairs for each of the types. If no action * has to be defined then it action should be NULL. * @param string $towhat What to change this to * @param string $where Used only for 'requester', it should the name of the node after which to insert. If it is not found, * the new value is inserted as a new group. * @retval false It returns false if you are trying to use it without a database or XML file or if an element can't be found. * @retval true * */ public function editACL($type, $what, $towhat, $where = FALSE) { if (is_null($this->prefix)) { return false; } if (is_string($what)) { $req = table::findS($this->prefix . "_{$type}s", array($type => $what)); if (!$req) { trigger_error("Can't find {$what} of {$type}"); return false; } $what = $req->id; } if ($type == 'resource' || $type == 'action') { $req = new table($this->prefix . "_{$type}s", $what); $req->{$type} = $towhat; $req->save(); } if ($type == 'requester') { $req = new table($this->prefix . '_requesters', $what); if (!$req) { trigger_error("Can't find {$what} {$type} to edit"); return false; } //Treat cases where $where is another requester, another id, or it is not defined/found if ($where) { if (is_numeric($where)) { $orig = new table($this->prefix . '_requesters', $where); } else { $orig = $req->find($this->prefix . '_requesters', array('requester' => $where)); } if ($orig) { $right = $orig->right; } else { trigger_error("Can't find {$where} to put there the {$what} {$type}"); return false; } rolisz::get('dbCon')->query("UPDATE `{$this->prefix}_requesters` SET \n\t\t\t\t\t\t\t\tleft = left-2 WHERE left>{$req->right} AND left<{$right}+3"); rolisz::get('dbCon')->query("UPDATE `{$this->prefix}_requesters` SET \n\t\t\t\t\t\t\t\tright = right-2 WHERE right>{$req->right} AND right<{$right}+3"); $req->left = $right + 1; $req->right = $right + 2; } $req->requester = $towhat; $req->save(); } if ($type == 'permission') { //Loop through each type and if it's a string, get the associated id foreach ($towhat as $type => $value) { if (is_string($value)) { $towhat[$type] = rolisz::table($this->prefix . "_{$type}s")->find($this->prefix . "_{$type}s", array($type => $value)); if (!$towhat[$type]) { trigger_error("{$value} {$type} not found"); return false; } $towhat[$type] = $towhat[$type]->id; } } $perm = new table($this->prefix . '_permissions', $what); foreach ($towhat as $type => $value) { $perm->{$type} = $value; } $perm->save(); } return true; }
/** * Magic method for setting or getting related tables. If function is called without argument, it will find the related * tables. It will automatically save the connection! * @param string $name * @param array $args * @todo $args could be a table of $name */ public function __call($name, $args) { if (!isset(self::$relations[$this->table][$name])) { throw new Exception($name . ' is not related to this table'); } if (empty($args)) { return $this->find($name); } $args = $args[0]; if (is_numeric($args)) { $args = array(self::$primaryKey[$name] => $args); } if (is_array($args)) { //Contains the link between our table and the other table, or if M2M relation, to connector table $relation = self::$relations[$this->table][$name]; if ($relation->type == ONE_TO_MANY) { $aux = new table($name); $aux = $aux->find($name, $args); if ($aux == NULL && key($args) != self::$primaryKey[$name]) { foreach ($args as $key => $value) { $aux->{$key} = $value; } $aux->save(); } $tK = $relation->targetKey; $oK = $relation->origKey; // @todo if $aux is array // @todo if $aux is NULL if ($relation->origKey == self::$primaryKey[$this->table]) { $aux->{$tK} = $this->{$oK}; $aux->save(); } elseif ($relation->targetKey == self::$primaryKey[$name]) { $this->{$oK} = $aux->{$tK}; } } elseif ($relation->type == MANY) { $aux = new table($name); $aux = $aux->find($name, $args); if ($aux == NULL && key($args) != self::$primaryKey[$name]) { $aux = new table($name); foreach ($args as $key => $value) { $aux->{$key} = $value; } $aux->save(); } //Contains the link between the connector table and the table we want to connect to $relation2 = self::$relations[$name][$this->table]; $connector = new table($relation->connector); $tK1 = $relation->targetKey; $tK2 = $relation2->targetKey; $oK1 = $relation->origKey; $oK2 = $relation2->origKey; $connector = $connector->find($relation->connector, array($tK1 => $this->{$oK1}, $tK2 => $aux->{$oK2})); if (!$connector) { $connector = new table($relation->connector); $connector->{$tK1} = $this->{$oK1}; $connector->{$tK2} = $aux->{$oK2}; $connector->save(); } } } elseif (is_object($args)) { //@todo } else { throw new Exception('Invalid argument called'); } }
public static function addAccess($accessWord, $userId = 0) { if (isset(self::$userTable) == false) { self::$userTable = new table(self::$userTableName); } if ($userId == 0) { $userId = self::$userTable->id; } if (self::hasAccess($accessWord, $userId) == false) { // add it... $aclTable = new table(self::$aclTableName); $aclTable->id = 0; $aclTable->user_id = $userId; $aclTable->access_word = $accessWord; $aclTable->save(); } }