On Symfony 6, we can easly share an entty object through several fixtures class with the methods addReference and getReference. But theses methods handle only entities class managed by the entityManager.
If I create a simple value in a variable (an integer, a string) initialized during the first fixture, how can I pass the variable to the next fixtures called during the ./bin/console d:fixture:load script ?
Thanks for any help !
addReference() and getReference are accessible by all your fixture classes, because they are implemented in a common base class Doctrine\Common\DataFixtures\AbstractFixture.
If you want to share more than that, just make use of the same idea:
Introduce your own parent class into the hierarchy and store everything you want to share on that parent:
abstract class MyFixturesParent extends AbstractFixture
{
protected int $mySharedInt = 1;
}
and implement your Fixtures as children of that parent
class MyFixtures extends MyFixturesParent
{
}
Related
As it is described thoroughly in the propel documentation, propel generates a base class, e.g., Base\Foo that is extended by an individual class Foo. The same applies to another another table, e.g., Base\Bar and Bar respectively. I could add the necessary attributes/methods to the Base classes but they can be regenerated. By using a middle-class
class Foo extends MyClass {}
class MyClass extends Base\Foo
{
//must be created for each Foo, Bar, Buzz,
//they must extend Base/Foo, Base/Bar, Base/Buzz respectively
}
What is the best way (in terms of propel) to have the single extendible class? Should behaviours be used?
I've been trying to build a PDO extension, and I wanted to have special classes in different files but I wanted to have them all link to the same original class.
I have worked with some Frameworks and I see that they use the extends class keyword, and I thought that it added the class you are making to the class that you have given.
Some code I have tried is:
class PSMQuery extends PSM {
// Functions and Jargon
}
I tried making an object for the original PSM class:
$psm = new PSM(/*Information*/);
But when I call the $psm variable like $psm->functionInTheExtendedClass it comes up with an error saying that it was an undefined method when I called it.
Am I using the extends keyword incorrectly?
Am I using the extends keyword incorrectly?
You use it correctly, but it works the other way around.
If PSMQuery extends from PSM, this means you can access and use stuff from PSM in PSMQuery, but not the other way around.
I think to understand it you can use a good example:
class twoRoomApartment extends building { }
So now you can think logical and already see, that a two room apartment probably extends from a building and not the other way around.
Means now in your code, you just create an instance of PSMQuery.
It's inheritance.
Parent : PSM
Child : PSMQuery
When you use extends you are extending parent class functionality and creating child class.
Your child class will inherit all the parent class functionality.
Parent will not get child class functionality.
So when u r trying to create object of parent class it doesn't know child class functionality.
You need to create object of child class and then you can access methods from parent class.
I haven't used abstract classes much in practice, though I understand what they are : a way to dictate to subclasses which methods must be implemented.
I just put a Kohana project through Doxygen and can see the class hierarchy in its entirety. I see that, at the top of the chain we have a factory:
abstract class Kohana_Model {
public static function factory($name){
// Add the model prefix
$class = 'Model_'.$name;
return new $class;
}
}
Inherited directly below that, we have an empty abstract class:
abstract class Model extends Kohana_Model {}
... And below that, there are three inherited classes: Kohana_Model_Database, Kohana_ORM, and Model_Foobar.
Would someone please explain the programming reasoning for this - what is the purpose of having an empty abstract class this high up in the chain? (and, at all?)
Why not have Kohana_Model_Database, Kohana_ORM, and Model_Foobar inherit directly from Kohana_Model, when there is (apparently?) no other branching or inheritance going on between Model and Kohana_Model?
Answers you're seeking for are Cascading File System and Transparent Extensions.
It allows you to create a model by calling
class News_Model extends Model
by default, and that will automatically then extend Kohana_Model and things will be hunky dory.
It also lets you extend Kohana_Model by creating your own Model file
class Model extends Kohana_Model
which overrides the abstract Model class, and allows you to add custom functionality. Then, when you upgrade your Kohana version to (say) 3.4, your extended Model doesn't get overwritten by the new Kohana files.
I'm building a class that interacts with an API. Say the API has a "get_something" method for "foo" objects and "bar" objects. I want my class to expose a "get_something" method, but be able to distinguish if it's for "foo" or for "bar."
What's a good solution for me? Can I create a class that has multiple name spaces? Would that be a good idea?
Maybe I should have nested classes?
you should look at namespaces as packages in java. a class belongs to 1 package only
what you can do though is have
namespace A_NS;
class A { ...}
namespace B_NS;
class A extends \A_NS\A {};
in that case class A will exist under namespace A_NS, and another class A' will exist under B_NS, which will extend class \NS_A\A;
you could check an object's class, or implement some identifier inside to distinguish.
overall, i would recommend that the design of your system will treat each class as if it belongs to 1 namespace only.
Two ways to do this, you can use an interface (http://us3.php.net/interface), which ensures that classes implementing the interface will have specified methods. Or create a parent class with abstract method (http://us3.php.net/abstract) get_something which will force any child classes to also have the class. Then you should be able to do a get_class($object) to determine which class was instantiated.
my models in CI are set up so that they load "sub"-models whenever they need certain functions. In order to keep my code as accessible and clean as possible, I want those submodels to extend the model they are called to.
So if I have two models:
<?php
class Mymodel extends Model
{
}
And:
<?php
class Submodel extends Model
{
function test() { do something.. }
}
Then I need to, somehow, be able get the submodel to extend mymodel, so that I can do something like $this->mymodel->test(). It doesn't have to be mymodel that submodel extends, it could be any model. Any ideas?
Thanks for your time.
You have an incorrect understanding of inheritance between classes.
Inheritance only flows one way, Down.
if Myodel extends Submodel your $this->mymodel->test() would work, but it does not make sense as sub (child) objects are suppose to inherit from parent objects, not the other way around.
As an analogy, you wouldn't look at a child and tell the parent, "You look just like your child", because it is the child that is a part representation of the parent.
you need to take the word extends very literally, you are literally 'extending' the functionality of the parent.
===================
One way i believe you could accomplish this is to create ghost functions that simply load the proper model and call that models function (though I do not recommend this as it could get very confusing for debugging.
per your example
<?php
class Mymodel extends Model
{
function test() {
$this->load->model('submodel');
$this->submodel->test();
}
}
Submodel
<?php
class Submodel extends Model
{
function test() { do something.. }
}
BUT again, if you are going for clean code, this is NOT the way to go, try and observe inheritance, and design your data with that in mind.
You can create utility model which may extends codeigniter's model and then all your models can extend that utility model. The methods you are going to add to the utility model will be available to all it's child classes aka your models.
How can you call a method which your class does not have or does not inherit from any other classes? Looking at your code, there is no relationship in your classes between Mymodel and Submodel.