SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 64
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Newbie - Connections Between Classes - Need a simple example

    Intro:
    Ok. I'm new to PHP, PDO, OOP, all new here.
    I've read a lot the last weeks, I don't mind to keep on reading.
    Sometimes, however, we must search for direction.
    This post request some directions and a simple example if possible.


    Scenario:
    I've done a insert to database page with a insert on a FK.
    On the "insert_site.php" page, I have a select box, where I store the "client table" ID's to be stored as fk on the "site table".
    So when you insert a site, you will have a client-id associated.

    I have another .php page, that I call, "process_insert_site.php", and that page, receives de $_POST values from "insert_site.php" and inserts the values into the database.



    Goal:
    Pass all this methodologies to a more OOP way, and instead of having a "process_insert_site.php" I could have a site class, with not only insert but, all CRUD functions inside, related to site.


    What I'm Trying to achieve:
    I need to create two classes, one "clientclass" and one "siteclass". The point may be to call the "id_client" of the "clientclass" on the "siteclass" (that will be responsable for the insert).

    I'm aware of composition and inheritance methods.


    Questions:

    Does it make sense to relate classes like this?

    Both classes already inherit attributes and methods from "databaseConn Class". (so I believe no more extend is possible).

    Should we create an instance of the id_client method, on the siteclass?


    Any guides about how to relate those two classes considering this approach will be absolutely appreciated.


    Thanks a lot,
    Márcio

  2. #2
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. Now with code:

    PHP Code:
    class Nk_Site extends Nk_BaseDados
    {
        private 
    $id_site;
        private 
    $endereco_site;
        private 
    $modulos_acesso_site;
        
        public function 
    __construct() {
        }
            
        
    // setters
        
    public function setIdSite$id_site ){        
        
    $this->id_site $id_site;
        }     
        
        public function 
    setEnderecoSite$endereco_site ) {        
        
    $this->endereco_site $endereco_site;    
        }     
        
        public function 
    setModulosAcessoSite$modulos_acesso_site ){        
        
    $this->modulos_acesso_site$modulos_acesso_site;    
        }     
        
        
    // getters
        
    public function getIdSite(){
        return 
    $this->id_site;    
        }     
            
            public function 
    getEnderecoSite(){
            return 
    $this->endereco_site;    
        }
            
        public function 
    getModulosAcessoSite(){
        return 
    $this->modulos_acesso_site;    
        }
        
        
        
    //function insert
        
    public function insereSite(){
            
            
    //since this class extends basededados, that extends PDO, I believe I we can use the $this keyword...

        
    $this->prepare("INSERT INTO SITE (endereco_site, modulos_acesso_site, id_cliente) VALUES (?, ?, ?)");
            
            
        
    $this->bindParam(1$this->endereco_site);
        
    $this->bindParam(2$this->modulos_acesso_site);

        
    $this->bindParam(3/*[B]What should I put here?[/B]*/);
            
        
    //executa o query:
        
    $this->execute(); 

    QUESTION:
    What I cannot solve is, how can I put a reference to the id_cliente attribute/property from the class ClientClass, inside the bindParam 3 on this class?


    Thanks a lot once again,
    Márcio

  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many times you will have a Finder Class or method which will perform the lookup for you using a parameter:

    PHP Code:
    class ClientFinder {
      function 
    findById($id) {
        
    //...
        
    return $client;
      }

    perhaps your Site class will make use of this:
    PHP Code:
      $this->client ClientFinder::findById($this->client_id); 
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot, I will search info about finder classes.

    So... something similar do this:
    PHP Code:
    $this->bindParam(3, (myclientclass::getClientId())); 
    Will never work?



    Regards once again,
    Márcio

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    South Florida
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes you should do most of the stuff using OOP classes, then you can reuse those classes again and again.

    to do something like this i would create couple of classes

    1. Data access class, which will be core class communicating with your database (MySQL)

    2. Template class to generate HTML code

    3. Form generation class (this can be a extended class for Template), create a generic class which create form and handle form posts (this may be the most complex class, i would suggest you to look-up some PHP open source project which generate forms)

    this class may become little complex if you want to add form validation (you can do this by combining with javascript for client end and verify again at server side (never trust post coming to server from client end)


    You can learn lot of about some advance PHP OOP techniques from open source projects, download couple of open source projects like Joomla, PHPBB ...

  6. #6
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm aware of OOP advantages, and yes, form validation is on my wishlist. And I imagine the hard part that that will be...
    But I'm also newbie. Using Class Finder seems to be, a no simple solution.

    I'd like to make it simple, and I believe my request shouldn't be nothing to hard. I just want to find a simple way to access other classes properties from one class.

    I believe that, to access other classes properties, we should have defined a getMethod so that other classes may call it. Then we must link them somehow, and access that attribute returned on the getMethod...

    This is a newbie question... I believe...


    Regards,
    Márcio

  7. #7
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    South Florida
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to have tree structure for your classes, which ones are the root classes and which ones are the smalled child classes which depend on roots

    more like a tree structure

    from this method, when you creating the child classes, at the class initializing part you can pass root classes as reference variable

    Code:
    class ChildClass {
    	
    	// Database Internal class pointer
    	var $_TDB = NULL;
    
            // Initialize Class
    	public function ChildClass(&$TDB)
    	{
    		$this->_TDB = $TDB;
    	}
    
    }
    as above example, this child class accessing database class. you can call any function in database class by using internal pointer $this->_TDB

    hope this helpful

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Note that as of PHP5 (and there is no excuse for using PHP4) you don't need to declare object variables as pointers, so you wouldn't need the & infront of the $TDB.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    South Florida
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Note that as of PHP5 (and there is no excuse for using PHP4) you don't need to declare object variables as pointers, so you wouldn't need the & infront of the $TDB.
    Good to know this, thanks

  10. #10
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes... but the problem is that I cannot inherid another class. Because, ClassSite already extends ClassDabase. So I cannot extend ClassSite to ClassClient...

    ClassClient and ClassSite are at the same level...

    Ok... I'm obviously not getting there...

    Sorry for wasting your time, I need to sleep and maybe tomorrow I can see clear how to solve this... :s

    Thanks a million,
    Márcio

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Site uses a Client so use composition.

    Then when the save method is called check to make sure the client exists. If it doesn't exist throw a exception and return.

    PHP Code:
    class Site {

          protected 
    $client;

           public function 
    __construct(Client $client=null) {

             if(
    $client$this->setClient($client);

          }
          
          public function 
    setClient(Client $client) {
          
              
    $this->client $client;
          
          }
          
          public function 
    getClient() {
          
              return 
    $this->client;
          
          }
          
          public function 
    hasClient() {
          
              return 
    is_null($this->getClient())?false:true;
          
          }
          
          public function 
    insereSite() {
           
               if(
    $this->hasClient()===false) {
               
                   throw new 
    Exception('A client must be declared to save a site. Exception thrown in class '.__CLASS__.' on line '.__LINE__.' inside method '.__METHOD__);
               
               }
           
          }



  12. #12
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    @OP

    I don't think extension is what you should be doing here. I'd say the Client should contain the database connection variable, and also sites as an array.

    The site class should also contain the data connection.

    For example:
    PHP Code:
    Class Client{
        public 
    $Database;
        public 
    $ID;
        public 
    $Sites = array();
        function 
    __Construct(PDO $Database$ID){
            
    $this->Database $Database;
            
    $this->ID = (int)$ID;
            
    $this->LoadSites();
        }
        function 
    LoadSites(){
            
    $GetSites $this->Database->Prepare('SELECT ID FROM Sites WHERE Client = :Client');
            
    $GetSites->BindValue(':Client'$this->IDPDO::PARAM_INT);
            
    $GetSites->Execute();
            foreach(
    $GetSites->FetchAll(PDO::FETCH_ASSOC) as $Site){
                
    $this->Sites[$Site['ID']] = new Site($this->Database$this$Site['ID']);
            }
        }
    }
    Class 
    Site{
        public 
    $Database;
        public 
    $ID;
        public 
    $Client;
        function 
    __Construct(PDO $Database$Client$ID){
            
    $this->Database $Database;
            
    $this->ID = (int)$ID;
            
    $this->Client $Client;
        }

    etc.

    What I personally do, and it seems to work very well, is I have a mappercollection class. It holds (simplified) a categorymapper class and an articlemapper class.

    The categorymapper class has methods for finding a certain category, e.g. by its name, ID or a certain article ID etc. This first runs a query through its parent MapperCollection's database object, which grabs the information of all categories which DONT have an ID in the cache array. For each row, it instantiates a category object which is then put into a cache array. THEN, it runs a function which locates all objects in the cache from the original specs.

    The same goes with the ArticleMapper object.

    Each Article and Category object is given their MapperCollection's ID object upon instantiation. There's a static GetMapperCollectionByID() to get their actual mappercollection; I prefer reference by ID than actual object - makes things simpler. There is a function built into all similar objects for easier access of their mappercollection: GetMapperCollection(). For a category to get all it's articles, it simply uses:
    PHP Code:
    Function GetArticles(){
        return 
    $this->GetMapperCollection()->ArticleMapper->Find(array('Category' => $this->ID));

    This means that all objects are infact only related by the MapperCollection which they reference (by ID), so that say moving an article to another category is simply a case of changing it's category property and done.

    The cache idea also works supurbly - any one row is only ever loaded once in one run.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  13. #13
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by arkinstall
    I don't think extension is what you should be doing here. I'd say the Client should contain the database connection variable, and also sites as an array.
    Yep, you should be using composition for that as well. A Client and Site use a Connection to save themselves. The relationship is not a is a one so it doesn't make much sense to use direct inheritance.

    Rather then passing the connection to the constructor you could pass it to only the methods you need it. Neither way is really wrong, but what ever is easier for you.

  14. #14
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Back on track.

    First, to all the replys: A very big Thanks !


    @arkinstall
    Wow! Not that others can't use the solution you have provided, but I must slow down a little bit here. You don't imagine what have I read on the past week. Is to much information to properly process. I must leave your solution for another moons. I'm having 1 full week of OO only.

    So my big problem with all this was, and maybe still his, a "design class" problem.
    I wakeup today thinking about a "is a" / "has a" relations. And today, on sitepoint, here they are. So, according to this, what I need is a "has a" relation., hence, composition.

    1)
    I already have a class that deals with the PDO things, now he does only the connection, but I want that class, in the future, do deal with fetch, bindparam etc... all PDO related. So, I have extend that databaseclass to PDO.

    So my first problem is:
    How do I relate this class with other classes, that will need to be database related?


    2)
    After dealing with the connection, I need to deal, for now, with those two: Sites and Client class. I must use composition, now I have no doubts about it, and the solution iphy as provided, and ooz expanded, was about passing the values of one class, on a constructor of another class.
    But I'd like to reserve the constructor for initiation proposes, and having Client ID attribute on SiteClass may not be what I think about what a initiation should be since, I will not always need him on all SiteClass. (only on some functions I believe)...

    3)
    I'm already imagining how hard will be to understand the delete of a site that does not necessarily implies the delete of the client. However, if I want to delete a client, then I probably what to delete the related info for that client, sites included. Database related issue, fk related issues, and all that expressed on classes... O.o


    Well, this was only to organize Ideas, I will start with problem 1).


    Thanks a lot once again guys,
    Márcio

  15. #15
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem 1 is solved. I have the following:

    A myPDO class.
    A generalDAOclass. (an abstract class, that requires on the constructor the myPDO class)
    A dogDAO class that extends generalDAOclass (it deals with all the CRUD operations for the dog).

    Now the problem 2:

    I have this insertDog() method:


    PHP Code:
    $stmt $this->_dbh->prepare("INSERT INTO DOG (name_dog, race_dog, id_vet) VALUES (?, ?, ?)");
            
            
    $stmt->bindParam(1$this->getNameDog() );
            
    $stmt->bindParam(2$this->getRaceDog());
            
    $stmt->bindParam(3, ????????????????????);
            
             
            
    $stmt->execute();
        } 
    To make this insert method work, I need to fill the id_vet database column with some values.
    I cannot use $this->getIdVet() because this method is not in dog class, is on the veterinary class.
    So my question is:

    How can I access the getIdVet() that is on the veterinary class to put it on the insert dog method?

    Thanks a lot,
    Márcio

  16. #16
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Pass the Veterinary object, or the Veterinary ID to the method.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  17. #17
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot,

    So the steps for accomplish that may be:

    1) Make an include on my dog class, pointing to my vet class (they are separate files).

    2) Create a $guessVet attribute inside my dog Class.

    3) Instantiate the vet class inside my dog class (but outside the insertDog method).

    4) Call one attribute (in this case the IdVet) of my vet object and put it inside my insertDog method on my Dog class.


    Is this ok?


    Thanks,
    Márcio
    Last edited by oikram; Apr 19, 2009 at 12:17. Reason: add step 2...

  18. #18
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    $dog = new Dog();

    $dog->name 'rusty';
    $dog->breed = new Breed(6);
    $dog->vet = new Vet(2);

    $dog->save(); 
    Not to give you a hard time or anything but have you maybe considered doing something like that?

    In regards to your question though your a little off.

    Thanks a lot,

    So the steps for accomplish that may be:

    1) Make an include on my dog class, pointing to my vet class (they are separate files).

    2) Create a $guessVet attribute inside my dog Class.

    3) Instantiate the vet class inside my dog class (but outside the insertDog method).

    4) Call one attribute (in this case the IdVet) of my vet object and put it inside my insertDog method on my Dog class.


    Is this ok?
    1.) yes, that would probably be the easiest way. Not ideal, but based on your setup that will work.

    2.) No, the vet should be a property of the dog class.

    3.) No, the vet should be passed into the Dog class upon construction or a setter.

    4.) Upon calling the insertDog() method have it check whether the vet property is null. If it is throw a exception if the vet is required. Otherwise, proceed to insert the dog and get the id of the vet via a getter on the vet instance.

    The below code outlines the very basic concept.

    PHP Code:
    class Dog {

        protected 
    $vet;

        public function 
    setVet(Vet $vet) {
        
            
    $this->vet $vet;
        
        }
        
        public function 
    insertDog() {
        
            if(
    is_null($this->vet)) {
                throw new 
    Exception('A dog must be associated with a vet. Unable to insert dog.');
            }
            
            
    $vetId $this->vet->getId();
        
        }

    {


    class 
    Vet {

        protected 
    $id
        
        
    public function getId() {
        
            return 
    $this->id;
        
        }



  19. #19
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok... here we go...

    Inside the public method setVet, what's the meaning of this:
    (Vet $vet) ?



    Inside my vet.class I have already this:
    PHP Code:
    public function setVet($vet) {
        
         
    $this->_vet $vet

    Should I remove it? Should I have both? Or should I called it somehow on my dog class?


    Thanks,
    Márcio

  20. #20
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    By prefixing $vet with Vet, we ensure that only a Vet object (or descendant of) can be passed as a parameter to the method.

    This is called Type Hinting.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  21. #21
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Why would you have a setter for vet inside the vet class? Assuming that once you instantiate a vet object that object represents the vet entity or no? Do you mean that method is inside the Dog class?

    In regards to type hinting if you know that the vet property has to be a Vet instance you can safely program to its interface. Otherwise, you would need to check whether its a Vet instance each time before calling a method of the Vet class on it. By using Type Hinting you always know the public interface of the property. Otherwise, the property could be anything with any interface. Its good to get into the habit of type hinting. The purpose is to separate implementation from the interface and using type hinting supports that goal. Otherwise, you result in programming to a implementation. The problem with programming to a implementation is that it produces tightly coupled code that can be difficult to maintain, extend and trouble shoot down the line. That is why its smart to use type hinting and actual interfaces when you can (which is a separate discussion), supporting the same basic goals - separation of implementation and interface.

  22. #22
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the answers...

    Well:
    Quote Originally Posted by oddz View Post
    Why would you have a setter for vet inside the vet class?

    To be honest. I don't know. I was trying to follow a DAO pattern that I see as an example (here if someone cares:http://www.revistaphp.com.br/artigo.php?id=133)
    Maybe I should forget about that, and when I find the need I put it, or change accordingly.

    I will now try to code something... and see if I understand a little bit more about how classes and objects communicate...


    Regards,
    Márcio

  23. #23
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    You would have a Vet entity and Vet DAO. No vet setter in either one. In the vet entity class you would only have getter and setters for properties of that entity. In the vet dao you would have methods that move vet entity information between the domain and database. However, you would pass the vet entity to the methods inside the vet dao instance in order to do so.

    Very basic example using a gateway without error handling, but you should get the idea.

    PHP Code:
    class Vet {
        
        protected 
    $id;
        protected 
    $name;
        
        public function 
    setId($id) {
            
    $this->id $id;
        }
        
        public function 
    getId() {
            return 
    $this->id;
        }
        
        public function 
    setName($name) {
            
    $this->name $name;
        }
        
        public function 
    getName() {
            return 
    $this->name;
        }
        


    PHP Code:
    class VetDAO {

        protected 
    $db;
        
        public function 
    __construct(PDO $db) {
            
    $this->db $db;
        }

        public function 
    insert(Vet $vet) {
        
            
    $sql 'INSERT INTO vets (name) VALUES (?)';
            
            if(
    $stmt $this->db->prepare($sql)) {
            
                
    $stmt->bindParam(2,$vet->getName(),PDO::PARAM_STR);
                
                if(
    $stmt->execute()) {
                    return 
    true;
                }
            
            }
        
        }


    PHP Code:
    $vetDAO = new VetDAO($pdo);

    $vet = new  Vet();

    $vet->setName('Happy Animal Way');

    $vetDAO->insert($vet); 

  24. #24
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok...

    And now back to the dog example provided:

    PHP Code:
    class Dog {

        protected 
    $vet;

        public function 
    setVet(Vet $vet) {
        
            
    $this->vet $vet;
        
        }
        
        public function 
    insertDog() {
        
            if(
    is_null($this->vet)) {
                throw new 
    Exception('A dog must be associated with a vet. Unable to insert dog.');
            }
            
            
    $vetId $this->vet->getId();
        
        }

    {


    class 
    Vet {

        protected 
    $id
        
        
    public function getId() {
        
            return 
    $this->id;
        
        }


    According to this structure, (Vet and vetDAO classes),
    should the setVet method be on the Vet Class ?

    I mean, I only need the setID of the vet class...

  25. #25
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by oikram
    should the setVet method be on the Vet Class ?
    No, a Vet is a vet. A Vet can't have another vet can it? If Vet where to have a property named vet you would be saying that vet has a vet, which isn't true from my understanding of your structure.

    Quote Originally Posted by oikram
    I mean, I only need the setID of the vet class...
    Well, no you would have setters for every property of that class. However, the id is the only one that is necessary to resolve its association with a dog to keep it simple.

    This is where things become a little complicated. A Dog by itself is a Dog. A Vet by itself is a Vet. However, the moment you say a Dog belongs to a vet you need to have a way to resolve that association. The way the previous code resolves that association is by making the associated vet instance a property of the dog class. So a Dog actually has a vet which is expressed by its vet property.

    PHP Code:
    $dog = new Dog();
    $vet = new Vet();

    $dog->setVet($vet); // dog now belongs to this vet 
    If you post some of the tables I/others could show you the process to determine the actual code. However, until then all that can be presented is pseudo code based the concept.

    dogs
    -- id
    -- vet_id

    vet
    -- id


    Given that it can be said that a dog belongs to a vet and that association may resolved as dogs(vet_id)=>vet(id).

    Now I guess the question is whether to not a dog MUST belong to a vet. If a Dog MUST belong to a vet that in theory in can't exist without out it. So it would be appropriate to make sure that a Dog always has a Vet.

    With that said, vet_id isn't actually a property of the Dog class. Instead vet_id maps to vet. Then we can get its unique id by calling vet->getId().

    Hopefully this isn't to much. However, associations are a complicated matter.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •