Quantcast

Implementing factory patterns in CakePHP

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Implementing factory patterns in CakePHP

elitalon
Hi,

I've been reading a couple of articles about implementing factory patterns in CakePHP but I can't find a hint to solve my actual problem. There is one that tries to mimic these kind of patterns using behaviors, but it's not exactly what I'm looking for.

I am developing an application that implements several types of issues management: customer complaints, software bugs, medical reviews, etc. The core is built around a common set of models: Brand, Customer, Project and Issues. A customer can sign up for many brands. What varies from brand to brand are Project and Issue, so that there is a LocalProject and its ComplaintIssue, a SoftwareProject and its BugIssue, a MedicalCenterProject and its ReviewIssue, etc.

Factory patterns suggest to implement to an interface (i.e. using Project and Issue classes) and make object instantiation at runtime. Doing that it's relatively easy in controllers, e.g you can instantiate $this->Project in ProjectsController according to runtime conditions.

But things get complicated when a hierarchy of relationships is present, since model construction is made "behind the scenes" in deep associations. Is there a way to define a factory method in CakePHP that can be used to decide which concrete class should be used at runtime?

Thanks!!!

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Implementing factory patterns in CakePHP

stork
Possibly sounds like work for InheritableBehavior from https://github.com/CakeDC/utils and combination of "Single/Class Table Inheritance" for your models.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Implementing factory patterns in CakePHP

senser
In reply to this post by elitalon
Hi elitalon,

You got a interesting situation. Please provide any details of solving this.

Regards!

On Friday, June 1, 2012 10:04:13 PM UTC+3, elitalon wrote:
Hi,

I've been reading a couple of articles about implementing factory patterns in CakePHP but I can't find a hint to solve my actual problem. There is one that tries to mimic these kind of patterns using behaviors, but it's not exactly what I'm looking for.

I am developing an application that implements several types of issues management: customer complaints, software bugs, medical reviews, etc. The core is built around a common set of models: Brand, Customer, Project and Issues. A customer can sign up for many brands. What varies from brand to brand are Project and Issue, so that there is a LocalProject and its ComplaintIssue, a SoftwareProject and its BugIssue, a MedicalCenterProject and its ReviewIssue, etc.

Factory patterns suggest to implement to an interface (i.e. using Project and Issue classes) and make object instantiation at runtime. Doing that it's relatively easy in controllers, e.g you can instantiate $this->Project in ProjectsController according to runtime conditions.

But things get complicated when a hierarchy of relationships is present, since model construction is made "behind the scenes" in deep associations. Is there a way to define a factory method in CakePHP that can be used to decide which concrete class should be used at runtime?

Thanks!!!

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Implementing factory patterns in CakePHP

Steve Found
In reply to this post by elitalon
I think the point about Cake as it stands is that a model will return it's data and any data associated with it as a nested array, which may actually help in what you want.

If you defined your own Project and Issue interfaces with interfaces that extend these (LocalProject extends Project, Complaint extends Issue ), then any of your models can implement them since you have the generated source for those models. 2 Project and Issue behaviour classes could then give concrete implementations of the 2 interfaces...

So your model definition would become:

class LocalModel extends AppModel implements ProjectInterface, IssueInterface {
    public $actsAs = array( 'LocalProject', 'Complaint' );
    ...
}

class SoftwareModel extends AppModel implements ProjectInterface, IssueInterface {
    public $actsAs = array( 'SoftwareProject', 'BugReport' );
    ...
}


Then your factory would just need to create the right model at runtime and return it to a controller. The controller is happy since it is getting a Model and your software is happy since the Model is also a Project and Issue instance.

Just some thoughts :) But I may have missed the point.

Steve (Ratty)


On 01/06/12 20:04, elitalon wrote:
Hi,

I've been reading a couple of articles about implementing factory patterns in CakePHP but I can't find a hint to solve my actual problem. There is one that tries to mimic these kind of patterns using behaviors, but it's not exactly what I'm looking for.

I am developing an application that implements several types of issues management: customer complaints, software bugs, medical reviews, etc. The core is built around a common set of models: Brand, Customer, Project and Issues. A customer can sign up for many brands. What varies from brand to brand are Project and Issue, so that there is a LocalProject and its ComplaintIssue, a SoftwareProject and its BugIssue, a MedicalCenterProject and its ReviewIssue, etc.

Factory patterns suggest to implement to an interface (i.e. using Project and Issue classes) and make object instantiation at runtime. Doing that it's relatively easy in controllers, e.g you can instantiate $this->Project in ProjectsController according to runtime conditions.

But things get complicated when a hierarchy of relationships is present, since model construction is made "behind the scenes" in deep associations. Is there a way to define a factory method in CakePHP that can be used to decide which concrete class should be used at runtime?

Thanks!!!
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Implementing factory patterns in CakePHP

Steve Found
In reply to this post by elitalon
Sorry this was wrong...  you would have

interface IProject {}
interface ILocalProject extends IProject {}
interface ISoftwareProject extends IProject {}

interface IIssue {}
interface IComplaint extends IIssue {}
interface IBugReport extends IIssue {}

cllass LocalModel extends AppModel implements ILocalProject, IComplaint {
    public $actsAs = array( 'LocalProject', 'Complaint' );
    ...
}


class SoftwareModel extends AppModel implements ISoftwareProject, IBugReport {
    public $actsAs = array( 'SoftwareProject', 'BugReport' );
    ...
}

Your factory could then have 2 methods:

getProject() would return the model that implemented the correct IProject derived interface.
getIssue() would return the model that implemented the correct IIssue derived interface
 
So your code would be something like:

DoSomethingWithProject( factory->getProject() );

function DoSomethingWithProject( IProject $project ) {
    ....
}

and DoSomethingWithProject() would be free to call any function defined in the base project interface definition.


--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Implementing factory patterns in CakePHP

Ankit Patel-2
Can u please tell me what is the diff between containable and actAs 

Thank You

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Implementing factory patterns in CakePHP

Jeremy Burns | Class Outfit
Chalk and cheese really.

$actsAs (note the 's' after 'act' - common mistake) is a variable that defines any behaviours the model should use (act as). One such behaviour in Containable that allows you to define quite precisely what related models are included in your finds from this model.

Jeremy Burns
Class Outfit

http://www.classoutfit.com

On 15 Jun 2012, at 05:05:57, ankit patel wrote:

Can u please tell me what is the diff between containable and actAs 

Thank You


--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Implementing factory patterns in CakePHP

Ankit Patel-2
Ok Thank You SIr.

I more thing Can you please tell me following thing:


I have one table  messages 
I have also take one table book_favourites
I have also table book in which I have taken foreign Key user_id


Now in book_favorites table I have foreign keys book_id and user_id 

Same In messages table I have  foreign keys book_id and user_id ,but this are showing error like cannot add or update child row 

So is there any way Instead of taking fields book_id and user_id direct id of the book table 


Thank YOu





--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Loading...