public static function getTimeline($user, $offset = 0, $max = 20) { if ($user == NULL) { throw new Exception('user is null'); } // Messages belonging to me or the users I follow $_or = Condition::_OR()->add(Condition::EQ(self::TABLE, 'createdBy_id', $user->getId())); // I want to see my messages also $following = $user->getFollowing(); foreach ($following as $otherUser) { $_or->add(Condition::EQ(self::TABLE, 'createdBy_id', $otherUser->getId())); } // Paginated search // Messages ordered by createdOn, first the last messages $list = Message::findBy($_or, new ArrayObject(array('sort' => 'createdOn', 'dir' => 'desc', 'offset' => $offset, 'max' => $max))); return $list; }
/** * @param string username * @param string password * @param boolean remember */ public static function login($username, $password) { if (empty($username) || empty($password)) { return self::LOGIN_ERR_INCOMPLETE; } $cond = Condition::_AND()->add(Condition::EQ(self::TABLE, 'username', $username))->add(Condition::EQ(self::TABLE, 'password', $password)); $list = TUser::findBy($cond, new ArrayObject()); if (count($list) == 0) { return self::LOGIN_ERR_FAILED; } $user = $list[0]; // Uusario logueado queda en session YuppSession::set('_twitter_user', $user); $user->save(); // TODO: check 4 errors // TODO: se deberia llevar log de la IP+userid+fecha // Se podria hacer un archivo de log en disco por cada user id y poner fechas con ips nomas return self::LOGIN_ERR_SUCCESS; }
<pre> <?php include_once "core.db.criteria2.Condition.class.php"; include_once "core.db.criteria2.ComplexCondition.class.php"; include_once "core.db.criteria2.CompareCondition.class.php"; include_once "core.db.criteria2.BinaryInfixCondition.class.php"; include_once "core.db.criteria2.UnaryPrefixCondition.class.php"; include_once "core.db.criteria2.Query.class.php"; include_once "core.db.criteria2.Select.class.php"; include_once "../core.db.DatabaseMySQL.class.php"; $q = new Query(); $q->addFrom('Persona', 'p')->addFrom('Empleado', 'e')->setCondition(Condition::_AND()->add(Condition::EQ('p', "nombre", "Carlos"))->add(Condition::GT('p', "edad", 5))->add(Condition::EQA('p', "nombre", 'p', "nombre2"))); $db = new DatabaseMySQL(); echo $db->evaluateQuery($q); //echo Condition::EQ("per", "nombre", "Carlox")->evaluate(); //echo Condition::EQA("per", "nombre", "per", "nombre2")->evaluate(); /* $sel = new Select(); $sel->addProjection("per", "name"); $sel->addProjection("per", "age"); $sel->addAvg("per", "age"); echo $sel->evaluate(); */ $q = new Query(); $q->addAggregation(SelectAggregation::AGTN_DISTINTC, 'datos', 'nombre')->addFrom('Persona', 'datos')->setCondition(Condition::GT('p', "edad", 5)); echo $db->evaluateQuery($q); ?> </pre>
/** * Se usa solo desde PO::aRemoveFrom y PO::aRemoveAllFrom. * * ES COMO LO CONTRARIO DE SAVE_ASSOC, pero para solo un registro. save_assoc( PersistentObject &$owner, PersistentObject &$child, $ownerAttr ) * Elimina la asociacion hasMany entre los objetos. (marca como eliminada o borra fisicamente el registro en la tabla de join correspondiente a la relacion entre los objetos). * attr1 es un atributo de obj1 * attr2 es un atributo de obj2 * attr1 y attr2 corresponden a los roles de la misma asociacion entre obj1 y obj2 * attr1 y/o attr2 debe(n) ser hasMany * logical indica si la baja es fisica o logica. */ public function remove_assoc($obj1, $obj2, $attr1, $attr2, $logical = false) { Logger::getInstance()->pm_log("PM::remove_assoc"); // TODO: Si la relacion es A(1)<->(*)B (bidireccional) deberia setear en NULL el atributo A y A_id de B. // Veo cual es el owner: $owner =& $obj1; $ownerAttr =& $attr1; $child =& $obj2; if ($obj1->getClass() != $obj2->getClass() && $obj2->isOwnerOf($attr1)) { $owner =& $obj2; $ownerAttr =& $attr2; $child =& $obj1; } Logger::getInstance()->log('PM::remove_assoc owner ' . $owner->getClass() . ', child ' . $child->getClass()); // Para eliminar no me interesa el tipo de relacion (si esta instanciada bidireccional o unidireccional). // Quiero eliminar el que tenga ownerid y childid de los objetos que me pasaron. // (obs: entonces no permito mas de una relacion entre 2 instancias!) ); // El id de la superclase, es igual que el id de la clase declarada en el hasMany, y el mismo que la instancia final // Por eso uso el id del objeto directamente $ref_id = $child->getId(); Logger::getInstance()->log('PM::remove_assoc owner_id ' . $owner->getId() . ', ref_id ' . $ref_id); // se pasan instancias... para poder pedir el withtable q se setea en tiempo de ejecucion!!!! // $tableName = YuppConventions::relTableName($owner, $ownerAttr, $child); // Necesito el id del registro para poder eliminarlo... // esto es porque no tengo un deleteWhere y solo tengo un delete por id... (TODO) YuppLoader::load("core.db.criteria2", "Query"); $q = new Query(); $q->addFrom($tableName, "ref")->addProjection("ref", "id")->setCondition(Condition::_AND()->add(Condition::EQ("ref", "owner_id", $owner->getId()))->add(Condition::EQ("ref", "ref_id", $ref_id))); $data = $this->dal->query($q); $id = $data[0]['id']; // Se que hay solo un registro... // TODO: podria no haber ninguno, OJO! hay que tener en cuenta ese caso. $this->dal->deleteFromTable($tableName, $id, $logical); }