As I am not sure, is it possible to create models with DB Class instead of Eloquent? I want to stay away from ORM.
Thanks
Yes of course its possible. You dont need to extend any class to make a model class that encapsulates business logic and consists of methods calling the DB class.
Just create your model inside app/models/MyModel.php like this
class MyModel{
public static function getMyData(){
return DB::table('users')->select('column')->get();
}
}
then you should be fine to call your new class statically:
$data = MyModel::getMyData();
If you wanted to extend the DB class you could, though more likely you would be looking to extend the Database/Builder class to extend functionality but this is a complex topic and I suspect you would have asked a very different question if this was what you were after.
As I final note, I wouldn't steer clear of Eloquent, it's the greatest thing about Laravel amongst a lot of other great things
Just remove the "extends Eloquent" and build the queries using the DB class.
Related
I just want to know what would be better between below two scenerio.
I have model A and B in CodeIgniter and two different controllers for interacting with those models, That is A_controller and B_controller, Now my question is i have so many of methods defined in both model A and B. Now i want to use a method of model A in A_controller.
It would be better to load whole model A into B_controller to just use a single method, or should i make a separate method in model B for use in B_controller.
Which option will be better with respect to Performance.
Any help will be appreciated!
Divide and conquer
create a small model with the shared functions between models A and B, this way you won't have any duplicated code anywhere. And then you load this new model and the model A for example.
Controller A
$this->load->model('A_model');
$this->load->model('Shared_model');
Controller B
$this->load->model('B_model');
$this->load->model('Shared_model');
Hope that helps
Create one single model file and write functions, so you need to load only one model in controller. All functions can access from a single model.
Nice answer provided by David above. you can also check some good answer for the same
Codeigniter : calling a method of one controller from other
stackoverflow.com/questions/6500022/codeigniter-calling-a-method-of-one-controller-from-other
I'm just learning Laravel 4 and I was unsure about how Eloquent models work. It is clear to me that they are a good way to interact with the database but can I define constructors and functions on a class that extends Eloquent to use it for more than database interaction?
Yes! A class that extends Eloquent, doesn't make it a class solely used for Eloquent purposes. You've got the realize the concept of class extension. Class extension is there to simply add features to ANY class from another, existing class. In this case Eloquent.
You can take any model you already have, and tomorrow decide that you want to use it with Eloquent and simply extend it. Your original class is still your original class and works the same. Any methods or properties in the original class will ovewrite its parent (Eloquent) if the parent has something with the same name.
In fact, creating other methods in a class that extends Eloquent is the real way to create robust models that do all sorts of cool stuff. I create methods in my User model for example, to calculate how many day's until their birthday. Instead of pulling the birthday column into a controller then using PHP to do the calculation, I just have a method like User::daysUntilBirthday();
If you use a constructer and extend Eloquent, make sure you still fire off Eloquent's constructor as well though.
class MyModel extends Eloquent {
public function __construct($attributes = array(), $exists = false)
{
parent::__construct($attributes, $exists); // This will fire off Eloquent's constructor.
// Your construct code.
}
}
I want to add a function that will be used by several fixtures in my app, and it would be nice to be able to put it somewhere reflecting that it's an extension to CakeTestFixture. In a Model, I could do this by writing a Behavior or adding it to app_model.php. Is there a way to do this for fixtures?
I can always just add it to my static Utility class, but it would be nice to be able to structure it better.
Write an AppTestFixture which extends CakeTestFixture and include it in there. Obviously each fixture then has to extend your new class instead of Cake's one.
I am a 4 days old yii fan and I like it so much.
I have a special database table that can't be used by CActiveRecord directly. My solution is to subclass CModel to have my own logic for listing, creating, saving and deleting records. My new CModel subclass cant not instantiated; it seems that CModel requires more methods to be defined to allow creating an instance from it.
My question is: Is this the right approach to go or there are better ways? If yes, what are the missing methods to define to make my new class complete; not abstract
Thanks
I usually create my own classes to handle the so called 'logic' of the webapp that I'm building.
I place it in another folder (usually the logics folder) and auto import the directory from the config. The logic classes doesn't subclass from any Model
public class ProfitLogic { ... }
where inside the class(es) I implement functions that instantiates and use any ActiveRecord(s) that I need.
The reasoning for this is that when prototyping I often refine my database design, and I need to regenerate the ActiveRecords again :p
Your approach is fine generally speaking, and would be fine even if you were not "forced" to adopt it. I use a CActiveRecord subclass as the base for my models to provide additional custom functionality.
As for your other question, you only need to implement attributeNames() to be able to instantiate objects of your class.
However, why do you not subclass CActiveRecord directly instead of CModel? You can still override any and all methods you choose to. Is your database so dramatically different from the usual schemas that you won't be able to reuse any of the logic?
I'm fairly new to Yii as well, but have found that extending CForm, as in the default ContactForm model can be useful.
Not the best for having lots of heavy business logic, but it touches on your point of breaking out of the typical workflow.
I need help in designing my PHP classes where I need to extend from multiple classes.
I have a general class, Pagination.php that does all sort of pagination and sorting. All other classes will use this for pagination.
To make my life easier, I made a class generator that generates a class from MySQL table. All the properties, getters, setters and common methods are created automatically, which really saves time and money.
As an example, class Staff_Base in Staff_Base.php is generated automatically from SQL table t_staff.
Since class Staff_Base is automatically generated from SQL table, any 'custom' methods / properties are located in another class that extends Staff_Base.php. (So that whenever a new field is added, I can simply regenerate Staff_Base class and overwrite in Staff_Base.php).
So I have class Staff.php that extends Staff_Base.php.
The problem is, Staff.php also needs to extend another class, Pagination.php.
(The current workaround is to put methods in Pagination.php into every class. This is really troublesome whenever I make changes to the pagination/sorting methods.)
How do I do this?
What is the best design pattern to achieve this?
I know common suggestions to restructure my classes, but I really think hard of other workaround/solution. Also, I may also need to extend other classes than Pagination.php.
Thanks!
Can you have your generated Staff_Base class inherit from Pagination? Or does Staff_Base already inherit from another base class (that you do not have control over)...
Sounds like either Doctrine or Propel, I do not recall which uses the *_Base class system.
My suggestion would be to rewrite pagination to be able to be used by your entity classes instead of requiring your entity classes to extend it.
So if I am reading what you wrote correctly, since you can't inherit from 2 classes you are duplicating paginate into every class you have.
Class stacking is a solution. One of the first things I googled.
I would recommend changing your Staff_Base.php generator to make that class extend Pagination by default. That way Staff extends Staff_Base, and Staff_Base extends Pagination. I think that's probably the cleanest (and most object-oriented) way of getting the results you want.
you cant, multiple inheritance is not supported in php, but if you do a google search on this topic you can find some workarounds...
It sounds like you're mixing things up here. A class (such as a Staff class) is used to represent a single entity. Eg:
$john = new Staff('John');
How exactly does the paging fit into this? Being page-able (paginatable?) sounds like a property of whatever it is that allows access to these Staff entities, not of the entity itself. That way, the way is clear for each type of Staff class you create to inherit from the base class.
So, what I believe would be the solution you need:
A Staff class (Staff_Base, and its graph of children)
A Staff Data Access Object (DAO\Staff would be a nice name, if you're using namespaces)
An Interface, to signal to the world that a DAO can be paged
Import to note is that there is no direct inheritance between the DAO class and the Staff class. You can still generate the Staff_Base class based on its properties in the database, and extend from there... as long as you don't include the actual data access in that class.
The code using this would then look something like this:
<?php
$staffDao = new DAO\Staff;
$staffMembers = $staffDao->getPagedResult($start, $amount);
?>
Edited to emphasize that the inheritance structure should be separate from the actual retrieval
Well, you might already know that PHP doesn't support multiple inheritance. One way around might be using Interfaces instead of superclasses, although, if the logic is identical for each implementing of the interface, this might become tedious. How about writing a code generator, that simply injects the methods to each class? You seem to already do that on the "common methods".
Oh, and using getters and setters (as they are used in e.g. Java) in PHP is considered not a good idea. Objects are slow as they are, so using public fields is considered the norm.
Edit: Then there's the __call()-hack, which could recognize the methods that actually reside in your other classes, and call them manually.