IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Zend Framework PHP Discussion :

Doctrine et foreign key


Sujet :

Zend Framework PHP

  1. #1
    rib
    rib est déconnecté
    Membre confirmé
    Inscrit en
    Janvier 2005
    Messages
    70
    Détails du profil
    Informations forums :
    Inscription : Janvier 2005
    Messages : 70
    Par défaut Doctrine et foreign key
    bonjours j'ai deux tables mysql reliées par une foreign_key, j'ai générer mes modèles avec doctrine:
    Artists.php:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
     
    <?php
    // Connection Component Binding
    Doctrine_Manager::getInstance()->bindComponent('App_Models_Artists', 'doctrine');
     
    /**
     * App_Models_Base_Artists
     * 
     * This class has been auto-generated by the Doctrine ORM Framework
     * 
     * @property integer $id_artist
     * @property string $nom
     * @property string $id_facebook
     * @property string $bio
     * @property integer $ordre
     * @property Doctrine_Collection $ArtistsHaveLinks
     * 
     * @package    ##PACKAGE##
     * @subpackage ##SUBPACKAGE##
     * @author     ##NAME## <##EMAIL##>
     * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
     */
    abstract class App_Models_Base_Artists extends Doctrine_Record
    {
        public function setTableDefinition()
        {
            $this->setTableName('artists');
            $this->hasColumn('id_artist', 'integer', 4, array(
                 'type' => 'integer',
                 'length' => 4,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => true,
                 'autoincrement' => true,
                 ));
            $this->hasColumn('nom', 'string', 20, array(
                 'type' => 'string',
                 'length' => 20,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('id_facebook', 'string', 30, array(
                 'type' => 'string',
                 'length' => 30,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('bio', 'string', 5000, array(
                 'type' => 'string',
                 'length' => 5000,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('ordre', 'integer', 4, array(
                 'type' => 'integer',
                 'length' => 4,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
        }
     
        public function setUp()
        {
            parent::setUp();
            $this->hasMany('App_Models_ArtistsHaveLinks as ArtistsHaveLinks', array(
                 'local' => 'id_artist',
                 'foreign' => 'id_artist'));
        }
    }
    et ArtistsHaveLinks.php:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
     
    <?php
    // Connection Component Binding
    Doctrine_Manager::getInstance()->bindComponent('App_Models_ArtistsHaveLinks', 'doctrine');
     
    /**
     * App_Models_Base_ArtistsHaveLinks
     * 
     * This class has been auto-generated by the Doctrine ORM Framework
     * 
     * @property integer $id_link
     * @property integer $id_artist
     * @property string $img_link_artist
     * @property string $url_link_artist
     * @property integer $ordre
     * @property App_Models_Artists $Artists
     * 
     * @package    ##PACKAGE##
     * @subpackage ##SUBPACKAGE##
     * @author     ##NAME## <##EMAIL##>
     * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
     */
    abstract class App_Models_Base_ArtistsHaveLinks extends Doctrine_Record
    {
        public function setTableDefinition()
        {
            $this->setTableName('artists_have_links');
            $this->hasColumn('id_link', 'integer', 4, array(
                 'type' => 'integer',
                 'length' => 4,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => true,
                 'autoincrement' => true,
                 ));
            $this->hasColumn('id_artist', 'integer', 4, array(
                 'type' => 'integer',
                 'length' => 4,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('img_link_artist', 'string', 50, array(
                 'type' => 'string',
                 'length' => 50,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('url_link_artist', 'string', 100, array(
                 'type' => 'string',
                 'length' => 100,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('ordre', 'integer', 4, array(
                 'type' => 'integer',
                 'length' => 4,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
        }
     
        public function setUp()
        {
            parent::setUp();
            $this->hasOne('App_Models_Artists as Artists', array(
                 'local' => 'id_artist',
                 'foreign' => 'id_artist'));
        }
    }
    ma classe artist a bien une Doctrine_Collection $ArtistsHaveLinks

    je comprend pas comment faire pour la peupler,
    se peuple elle automatiquement lors d'un select * sur la table artists dans ce cas comment accéder a la collection

    je souhaiterais récupérer un objet avec un vecteur pour les liens mais je c pas comment faire.
    Merci d'avance.

  2. #2
    rib
    rib est déconnecté
    Membre confirmé
    Inscrit en
    Janvier 2005
    Messages
    70
    Détails du profil
    Informations forums :
    Inscription : Janvier 2005
    Messages : 70
    Par défaut
    bon j'ai trouvé une solution:

    j'ai modifier ma classe App_Models_Artists comme ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
    class App_Models_Artists extends App_Models_Base_Artists
    {
    	private $link;
     
    	/**
    	 * @param $link
    	 */
    	public function setLink() {
    		$vue = Doctrine_Core::getTable('App_Models_ArtistsHaveLinks');
    		$listeLinks = $vue->fetchAll('id_artist= '.$this->id_artist,'ordre ASC',null,null);
    		$this->link = $listeLinks;
    	}
     
    	public function haveLink() {
    		if (count($this->link)>0){
    			return true;
    		}else{
    		return false;
    		}
    	}
     
    	public function getLink() {
    		return $this->link;
    	}
     
    }
    la classe App_Models_ArtistsHaveLinksTable:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     
    class App_Models_ArtistsHaveLinksTable extends Doctrine_Table
    {
        /**
         * Returns an instance of this class.
         *
         * @return object App_Models_ArtistsHaveLinksTable
         */
        public static function getInstance()
        {
            return Doctrine_Core::getTable('App_Models_ArtistsHaveLinks');
        }
     
    	public function fetchAll($where=null, $order=null, $limit=null, $offset=null)
    	{
    		$select = Doctrine_Query::create()
    			->select('*')
    			//dans le from on met le nom de la classe
    			//Doctrine s'occupe du reste
    			->from('App_Models_ArtistsHaveLinks');
     
    		if(isset($where)){ $select->where($where); }
    		if(isset($order)){ $select->orderBy($order); }
    		if(isset($limit)){ $select->limit($limit); }
    		if(isset($offset)){ $select->offset($offset); }
     
    		return $select->execute();
    	}
    }
    voici mon controleur ki genere la liste de mes artists avec leur liens:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
     
    class Artists_ListController extends Zend_Controller_Action {
     
    	public function indexAction() {
    		//on récupère la classe table de App_Models_Artists
    		$table = Doctrine_Core::getTable('App_Models_Artists');
    		//on liste tout le monde
    		$listeArtists = $table->fetchAll(null,'ordre ASC',null,null);
    		//on traite le résultat
     
    		foreach($listeArtists as $artist){
     
    			$artist->setLink();
    		}
     
     
     
    		$nbArtists = COUNT($MalisteArtists);
    		if ($nbArtists % 2 != 0){
    			$nombre_de_lignes = (int)($nbArtists/2)+1;
    		}else{
    			$nombre_de_lignes = $nbArtists/2;
    		}
    		$height_conteneur =  $nombre_de_lignes * 235;
     
     
    		$this->view->nbartists = $nbArtists;
    		$this->view->height_conteneur = $height_conteneur;
    		$this->view->artists = $listeArtists;	
    	}
    }
    com sa dans ma vue je recupere bien mes artists avec le ArrayObject contenant leur liens respectif.

    si quelqu'un connait quelquechose de plus simple pour obtenir le meme resultat je suis preneur.
    Merci d'avance

  3. #3
    rib
    rib est déconnecté
    Membre confirmé
    Inscrit en
    Janvier 2005
    Messages
    70
    Détails du profil
    Informations forums :
    Inscription : Janvier 2005
    Messages : 70
    Par défaut
    voila le yaml generé par doctrine grace a ma base de donnée:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    Artists:
      connection: doctrine
      tableName: artists
      columns:
        id_artist:
          type: integer(4)
          fixed: false
          unsigned: false
          primary: true
          autoincrement: true
        nom:
          type: string(20)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        id_facebook:
          type: string(30)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        bio:
          type: string(5000)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        ordre:
          type: integer(4)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
      relations:
        ArtistsHaveLinks:
          local: id_artist
          foreign: id_artist
          type: many
    ArtistsHaveLinks:
      connection: doctrine
      tableName: artists_have_links
      columns:
        id_link:
          type: integer(4)
          fixed: false
          unsigned: false
          primary: true
          autoincrement: true
        id_artist:
          type: integer(4)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        img_link_artist:
          type: string(50)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        url_link_artist:
          type: string(100)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
        ordre:
          type: integer(4)
          fixed: false
          unsigned: false
          primary: false
          notnull: true
          autoincrement: false
      relations:
        Artists:
          local: id_artist
          foreign: id_artist
          type: one
    la classe artist generée par doctrine a une Doctrine_Collection $ArtistsHaveLinks

    je vien de me rendre compte que je pouvait i aceder et qu'elle se peuplait automatiquement avec un select * sur la table.
    apparement doctrine rempli une Doctrine_Collection contenant les enregistrements liés par les Foreighn_key

    du coup voici ma classe App_Models_Artists:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    class App_Models_Artists extends App_Models_Base_Artists
    {
    	public function haveLink() {
    		if (count($this->ArtistsHaveLinks)>0){
    			return true;
    		}else{
    		return false;
    		}
    	}
    }
    voici la App_Models_Base_Artists:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
     
    abstract class App_Models_Base_Artists extends Doctrine_Record
    {
        public function setTableDefinition()
        {
            $this->setTableName('artists');
            $this->hasColumn('id_artist', 'integer', 4, array(
                 'type' => 'integer',
                 'length' => 4,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => true,
                 'autoincrement' => true,
                 ));
            $this->hasColumn('nom', 'string', 20, array(
                 'type' => 'string',
                 'length' => 20,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('id_facebook', 'string', 30, array(
                 'type' => 'string',
                 'length' => 30,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('bio', 'string', 5000, array(
                 'type' => 'string',
                 'length' => 5000,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
            $this->hasColumn('ordre', 'integer', 4, array(
                 'type' => 'integer',
                 'length' => 4,
                 'fixed' => false,
                 'unsigned' => false,
                 'primary' => false,
                 'notnull' => true,
                 'autoincrement' => false,
                 ));
        }
     
        public function setUp()
        {
            parent::setUp();
            $this->hasMany('App_Models_ArtistsHaveLinks as ArtistsHaveLinks', array(
                 'local' => 'id_artist',
                 'foreign' => 'id_artist'));
        }
    }
    voici la App_Models_ArtistsTable:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
     
    class App_Models_ArtistsTable extends Doctrine_Table
    {
        /**
         * Returns an instance of this class.
         *
         * @return object App_Models_ArtistsTable
         */
        public static function getInstance()
        {
            return Doctrine_Core::getTable('App_Models_Artists');
        }
     
    	/**
         * Select de base
         * 
         * @param string $where
         * @param string $order
         * @param int $limit
         * @param int $offset
         * @return Collection
         */
    	public function fetchAll($where=null, $order=null, $limit=null, $offset=null)
    	{
    		$select = Doctrine_Query::create()
    			->select('*')
    			//dans le from on met le nom de la classe
    			//Doctrine s'occupe du reste
    			->from('App_Models_Artists');
     
    		if(isset($where)){ $select->where($where); }
    		if(isset($order)){ $select->orderBy($order); }
    		if(isset($limit)){ $select->limit($limit); }
    		if(isset($offset)){ $select->offset($offset); }
     
    		return $select->execute();
    	}
    }
    mon controlleur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
     
    class Artists_ListController extends Zend_Controller_Action {
     
    	public function indexAction() {
    		//on récupère la classe table de App_Models_Artists
    		$table = Doctrine_Core::getTable('App_Models_Artists');
    		//on liste tout le monde
    		$listeArtists = $table->fetchAll(null,'ordre ASC',null,null);
    		//on traite le résultat
     
    		$nbArtists = COUNT($listeArtists);
    		if ($nbArtists % 2 != 0){
    			$nombre_de_lignes = (int)($nbArtists/2)+1;
    		}else{
    			$nombre_de_lignes = $nbArtists/2;
    		}
    		$height_conteneur =  $nombre_de_lignes * 235;
     
     
    		$this->view->nbartists = $nbArtists;
    		$this->view->height_conteneur = $height_conteneur;
    		$this->view->artists = $listeArtists;	
    	}
    }
    j'accede au liens de cette facon maintenant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
    <?php foreach($this->artists as $unartist) : ?>
    <?php if($unartist->haveLink()):?>
    			<span class="link_titre_liste_artists">
    			<img src="<?php echo $this->baseUrl(); ?>/images/artists/bouton_links_normal.png" />
    			</span>
     
    			<div class="boite_link_liste_artists">
    				<?php foreach($unartist->ArtistsHaveLinks as $link):?>
    					<span class="link_liste_artists">
    					<a href="http://<?php echo $link->url_link_artist; ?>" class="fade_img" target="_blank" ><img src="<?php echo $this->baseUrl(); ?>/images/artists/liens/<?php echo $link->img_link_artist; ?>" border="0" /></a>
    					</span>
    				<?php endforeach;?>
    			</div>
     
    			<?php endif;?>
    <?php endforeach;?>
    en fait se que j'avait fait avant, repeupler un vecteur liens dans la classe artists ne servait a rien car doctrine le faisait deja.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Doctrine] problème de foreign key errno150
    Par bor1s dans le forum ORM
    Réponses: 3
    Dernier message: 19/11/2010, 12h39
  2. [IB71] Je ne peux plus supprimer mes foreign key...
    Par BoeufBrocoli dans le forum InterBase
    Réponses: 3
    Dernier message: 19/09/2003, 14h39
  3. [postgresql][foreign key]
    Par elea1206 dans le forum Requêtes
    Réponses: 5
    Dernier message: 28/08/2003, 12h07
  4. [Foreign Key] Besoin d'explication.
    Par Andry dans le forum Débuter
    Réponses: 4
    Dernier message: 28/05/2003, 11h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo