tiny ‘n’ smart
database layer

Odkazy: dibi | API reference

Oznámení

Omlouváme se, provoz fóra byl ukončen

rozne entity z jednej tabulky – problem pri hasMany

stajo
Člen | 8

zdravim,

z tabulky users vytvaram rozne entity, ktore pouzivam v roznych castiach aplikacie: User, Player. zaregistroval som si filter, ktory mi do vysledku prida stlpec, pomocou ktoreho mapper zisti, aku entitu potrebujem (pridat stlpec do tabulky nepomoze – jeden zaznam moze ist aj na Player aj na User):

<?php
class RoleFilter {

    public static function markAsRole(\LeanMapper\Fluent $statement, $roleName) {
        $statement->select("%s AS role", $roleName);
    }
}

class Mapper {
    public function getEntityClass($table, \LeanMapper\Row $row = null) {
        if ($table == 'user') {
            if (isset($row->role)  &&  $row->role == 'player') {
                return $this->defaultEntityNamespace . '\Player';
            }
        }
        return parent::getEntityClass($table, $row);
    }
}
?>

, takze ak mam entity:

<?php
/**
 * Description of MatchOfPlayers
 *
 * @author stajo
 *
 * @property Player $oponent1 m:hasOne(player1_id:user) m:filter(markAsRole#player)
 * @property Player $oponent2 m:hasOne(player2_id:user) m:filter(markAsRole#player)
 */
class MatchOfPlayers extends Match {

}

/**
 * Description of Player
 *
 * @author stajo
 *
 * @property-read string $name (nick)
 * @property Team[] $teams m:hasMany(player_id:team_player:team_id:team)
 * @property Rating $actualRating m:belongsToOne(player_id) m:filter(order#date DESC, limit)
 * @property Rating $bestRating m:belongsToOne(player_id) m:filter(order#value DESC, limit)
 * @property Rating $worstRating m:belongsToOne(player_id) m:filter(order#value ASC, limit)
 */
class Player extends User implements \Model\IMatchOponent {

}

?>

, tak sa spravne priradi:

<?php
$match = $this->matchRep->find($id);
$match->oponent1->name                  //je tam to co, tam ma byt
?>

problem mam ale pri vazbe hasMany – property $players v tejto entite:

<?php
/**
 * Description of Team
 *
 * @author stajo
 *
 * @property int $id
 * @property string $name
 * @property string $title
 * @property Player[] $players m:hasMany(team_id:team_player:player_id:user)
 */
class Team extends \LeanMapper\Entity implements \Model\IMatchOponent {

}

//toto este ide
$team->addToPlayers($player);

//ale ked chcem prechadzat $players:
foreach($team->players as $player) {

}
//tak sa to chce namapovat na entity User.
?>

niekde v metode getHasManyValue sa vydoluju data bez pouzitia filtra, a mapper potom len na zaklade dat nevie vybrat spravnu triedu. pridat filter do m:hasMany tak ako to mam pre m:hasOne v entite MatchOfPlayers nejde. je nejaka ina moznost ako do referencovanej entity poslat info akeho typu je?