I use laravel.I want to create a class Frequest,which can produce an F21request ,F22request,F23request etc.These objects will have common attributes and each one can have different attributes also maybe with traits , which is the best solution , thanks in advance!
Related
I am looking for a way to add few custom methods which will be used in all models. I can imagine 3 ways of doing it :
Adding custom method to main Eloquent's Model.php class file(I want to avoid doing this as this is a core file)
Creating a custom model class with required custom methods, which will extend to eloquent's Model class and all the models in the project will extend to custom model class.
Adding a trait which will have my methods and include it inside all models
However, I want to do it more efficiently and best way possible. Is their any other way to do it?
PS I am using laravel 5.2 as its an old project.
Based on the comment discussion and adding my experience in Laravel I would suggest you to go either with #2 or #3 approach as #ceejayoz have specified in the comments
first one is definitely a bad approach as you need to modify the core which is not at all a good practice. Second and third are both good approaches.
But, before that you need to check your requirements if literally all models (including any future ones your app will ever have) need the extra functionality, however you can use traits for all models.
If I have the choice probably I will go for traits over custom models as traits are relatively simple then custom models
I'm a beginner in joomla. I create own component and would like to use model from other component (exactly contentbuilder).
I find few different ways how to create model but my problem is that
class ContentbuilderModelEdit extends JModel
use JPATH_COMPONENT_ADMINISTRATOR in it. When i create model ContentbuilderModelEdit i get warnings in lines that using JPATH_COMPONENT_ADMINISTRATOR constant.
Is it possible to create model using that constant from other component?
Thanks for your answers
Alas no. The JPATH_COMPONENT and JPATH_COMPONENT_ADMINISTRATOR are defined constants, and cannot be changed.
Sometimes the developers do this instinctively (it's easy) without realizing the kind of limitation they're putting on other developers. You might consider contacting the developers and propose such a change; if they accept, you won; if they don't, write a sed script that performs the changes (replacing it with JPATH_SITE and JPATH_ADMINISTRATOR . '/components/com_contentbuilder', and apply it after each update.
Or, copy their model into your component and rename it if it supports it.
:)
To call a model from another component you need firstly to include the path of this model:
JModelLegacy::addIncludePath(JPATH_SITE . '/components/comp1/models', 'Comp1Model');
Secondly you have to create an instance of your model:
$model = JModelLegacy::getInstance('Model1', 'Comp1Model');
After that you should be able to use the methods of your model.
ref link
I have done some programming with MVC but never really thought about class diagrams within them, but thinking about it I dont understand how a class diagram would work with one.
Would you just create a class diagram of each model,
Also, when I have used codenigter, i return query results from the model rather than a whole object, so would there even be any attributes?
Thanks
Would you just create a class diagram of each model
Anything that helps with clarity. I find its easiest to create a new diagram for each model.
Also, when I have used codenigter, i return query results from the model rather than a whole object, so would there even be any attributes?
Im not sure what you mean here? Why do you think your "results" are not objects? In pure OO everything (Integers, Strings etc) are objects too.
When designing a class diagram for a theoretical MVC-Pattern-based system, is it best to use List classes?
For example:
With List class:
Without list class:
I personally feel the latter is a better approach due to less code duplication, is there a better approach to this?
I don't think there is a need for a list class. Your controller can hold all references to the model.
Regardless of that, your UML models have a problem: in both cases you have an attribute customers:Customer[] and an composition link. This means that you have TWO references from the controller (or list) to the model. Either remove the attribute or the link.
I would suggest you to use swimlanes, one lane per structure component (MODEL / VIEW / CONTROLLER) and a separate lane for the database
a sample example would look like this
I have an dropdown attribute that I am creating during my module setup.
I want to pre-populate this attribute with some values while my module is installing. I can do this no problem, currently by simply storing the values in an array and then creating adding the options to the attribute in the install script.
Where would be the correct place to store these values - in a model? If so would it be a source model utilizing the toOptionArray method? This is technically used for forms so it doesnt seem right. But neither does just storing the values in the setup script.
Any ideas?
Yes, the toOptionArray method would be in line with standard Magento practices.
Typically the toOptionArray() is found in Helpers, if that is what you are asking. Helpers extend far fewer classes, and therefore inherit far fewer methods than models. This makes them much lighter weight for simple tasks like setting up an array of options, provided that they are static.
If the values are stored in a new DB table, and can be expanded upon by the user, it may make more sense to put this in a Model that has direct access to your DB table.