/** * Devuelve la lista de usuarios que pertenecen al grupo que pasa como parametro. * @param id_grupo, identificador del grupo. * @return array, lista de objetos de tipo usuario. * @version 17-02-09, 07-04-09 * @author Ana Martín */ public static function getAllUsuarios($id_grupo) { $c = UsuarioPeer::getCriterioNoBorrado(UsuarioPeer::FECHA_BORRADO); $c->add(UsuarioGrupoPeer::ID_GRUPO, $id_grupo); $c->addJoin(UsuarioGrupoPeer::ID_USUARIO, UsuarioPeer::ID_USUARIO); $c->addAscendingOrderBycolumn(UsuarioPeer::USUARIO); $lista = UsuarioGrupoPeer::doSelectJoinUsuario($c); $lista_usuarios = array(); foreach ($lista as $usuario_grupo) { $usuario = $usuario_grupo->getUsuario(); if ($usuario instanceof Usuario) { $lista_usuarios[] = $usuario; } } // print_r($lista_usuarios); return $lista_usuarios; }
/** * If this collection has already been initialized with * an identical criteria, it returns the collection. * Otherwise if this Usuario is new, it will return * an empty collection; or if this Usuario has previously * been saved, it will retrieve related UsuarioGrupos from storage. * * This method is protected by default in order to keep the public * api reasonable. You can provide public methods for those you * actually need in Usuario. */ public function getUsuarioGruposJoinGrupo($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN) { if ($criteria === null) { $criteria = new Criteria(UsuarioPeer::DATABASE_NAME); } elseif ($criteria instanceof Criteria) { $criteria = clone $criteria; } if ($this->collUsuarioGrupos === null) { if ($this->isNew()) { $this->collUsuarioGrupos = array(); } else { $criteria->add(UsuarioGrupoPeer::ID_USUARIO, $this->id_usuario); $this->collUsuarioGrupos = UsuarioGrupoPeer::doSelectJoinGrupo($criteria, $con, $join_behavior); } } else { // the following code is to determine if a new query is // called for. If the criteria is the same as the last // one, just return the collection. $criteria->add(UsuarioGrupoPeer::ID_USUARIO, $this->id_usuario); if (!isset($this->lastUsuarioGrupoCriteria) || !$this->lastUsuarioGrupoCriteria->equals($criteria)) { $this->collUsuarioGrupos = UsuarioGrupoPeer::doSelectJoinGrupo($criteria, $con, $join_behavior); } } $this->lastUsuarioGrupoCriteria = $criteria; return $this->collUsuarioGrupos; }
/** * Retrieve object using using composite pkey values. * @param int $id_usuario @param int $id_grupo * @param PropelPDO $con * @return UsuarioGrupo */ public static function retrieveByPK($id_usuario, $id_grupo, PropelPDO $con = null) { $key = serialize(array((string) $id_usuario, (string) $id_grupo)); if (null !== ($obj = UsuarioGrupoPeer::getInstanceFromPool($key))) { return $obj; } if ($con === null) { $con = Propel::getConnection(UsuarioGrupoPeer::DATABASE_NAME, Propel::CONNECTION_READ); } $criteria = new Criteria(UsuarioGrupoPeer::DATABASE_NAME); $criteria->add(UsuarioGrupoPeer::ID_USUARIO, $id_usuario); $criteria->add(UsuarioGrupoPeer::ID_GRUPO, $id_grupo); $v = UsuarioGrupoPeer::doSelect($criteria, $con); return !empty($v) ? $v[0] : null; }
protected function saveUsuario($usuario) { $usuario->save(); // Update many-to-many for "grupos" $c = new Criteria(); $c->add(UsuarioGrupoPeer::ID_USUARIO, $usuario->getPrimaryKey()); UsuarioGrupoPeer::doDelete($c); $ids = $this->getRequestParameter('associated_grupos'); if (is_array($ids)) { foreach ($ids as $id) { $UsuarioGrupo = new UsuarioGrupo(); $UsuarioGrupo->setIdUsuario($usuario->getPrimaryKey()); $UsuarioGrupo->setIdGrupo($id); $UsuarioGrupo->save(); } } }
/** * Populates the object using an array. * * This is particularly useful when populating an object from one of the * request arrays (e.g. $_POST). This method goes through the column * names, checking to see whether a matching key exists in populated * array. If so the setByName() method is called for that column. * * You can specify the key type of the array by additionally passing one * of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME, * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM. * The default key type is the column's phpname (e.g. 'AuthorId') * * @param array $arr An array to populate the object from. * @param string $keyType The type of keys the array uses. * @return void */ public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME) { $keys = UsuarioGrupoPeer::getFieldNames($keyType); if (array_key_exists($keys[0], $arr)) { $this->setIdUsuario($arr[$keys[0]]); } if (array_key_exists($keys[1], $arr)) { $this->setIdGrupo($arr[$keys[1]]); } }
/** * Operaciones de carga y busqueda de los grupos a los que pertenece el usuario * Borra un grupo o un array de grupos del array de grupos del usuario y lo borra tambien de la BD * */ public function borrarGrupo($grupo_s, $borrarArrayGrupo = true) { if (!is_array($grupo_s)) { $mis_grupos = array($grupo_s); } else { $mis_grupos = $grupo_s; } for ($i = 0; $i < sizeof($mis_grupos); $i++) { if ($this->_estaGrupo($mis_grupos[$i])) { if ($borrarArrayGrupo) { $this->_borrarGrupo($mis_grupos[$i]); } $migrupoUsuario = UsuarioGrupoPeer::RetrieveByPk($this->getIdUsuario(), $mis_grupos[$i]); $migrupoUsuario->delete(); } } }