Get all model details in zend - php

I have one doubt in zend framework. I need all model details from project which i have done in zend framework. Is there any possibility to get all model details in zend framework.
Please help me..
Thanks and regards,
Prasanth P

If by Model you mean you want to get the details about a DB Table, then yes. You can get this data by calling the following method from Zend_Db_Table
info() - Returns table information.
There is no method to say getAllDbTables. If you need this, you could write a custom Db Repository that knows all Db Tables and that can give you table instances and any info information you need about. This would have the additional benefit of decoupling concrete table instances from your using classes.
Keep in mind though that the M in MVC is not just the database. The M is the heart of your application. It contains application logic, domain objects, service layers, etc. The persistence layer is just one part of it.

Related

Laravel - Combining multiple Models to an array

a few years ago I wrote a PHP web application in a rather procedural way. As this project now gets reactivated and expanded I've made the decision to recode it using Laravel with its MVC structure. As I'm new to Laravel and MVC in general I'm working on converting my procedural approach in an object oriented approach.
After creating Models for each database table I'm faced with several questions:
When a page should be loaded that contains data from multiple models what is the best approach? Just call both models, get the data of both Models and send it to the view?
When I want to create a view that contains the aggregated data of multiple tables. In the actual version I just created a class that gets the data from both tables, adds some logic and returns the new array to the view. How would this be implemented in laravel? I didn't find some sort of wrapping model. Can you give me a hint how to implement this in laravel?
Thanks in advance for your help.
I would implement an Repository pattern for your problem. Take a look at my Repositories.
https://github.com/avored/ecommerce/tree/master/src/Models/Repository
At this stage my Repository doesn't contain multi table record fetch or store but eventually i will do it. Just for a starting point of view i hope it helps.

Do extremely simple tables still require their own model class?

I've been using laravel (php mvc framework) for a few weeks now. Currently I am creating a model for every single non-pivot table. Even tables as simple as:
id (unsigned int (PK)) | usertype (varchar(20))
1 | guest
2 | member
3 | Moderator
Because it makes it easier to relate my other models to them using the eloquent ORM.
I was wondering if its normal to create models for such simple tables for the sake of utilizing an ORM or if there is a better approach?
Currently my application is functioning using models for these tables, but I still want to make sure I am picking up good coding conventions while I'm learning.
Thanks in advance.
Here comes the long answer: YES!
I don't know when or why model became a synonym to entity, but this leads to confusion.
According to MVC inventor, Trygve Reenskaug, in the original MVC article:
DEFINITION
A Model is an active representation of an abstraction in the form of data in a computing system
[...]
The models are represented in the computer as acollection of data together with the methods necessary to process these data.
So, many people nowadays define (incorrectly) models as data representation, or the data storage or something else, but this is wrong.
Model deals with your application logic and this INCLUDES data abstraction, data storage, data processing, etc.
I use to call my data abstractions an Entity instead of model. My application model layer is called Service or Application Model. My storage layer is called just Storage and so on... All of this togheter is what we can call Model
That being clarified, now we can go on...
You said:
Because it makes it easier to relate my other models to them using the eloquent ORM.
That's enough!
Nothing keeps you away from using plan txt files as storage, but if your data relate to others, then you should look for a database.
Eloquent is an ORM (Object-Relational Mapping) and so, it relies on a database behind it. If you use txt files, how would do to recover users by their user type?
I'm not used to frameworks, but most of them generate entities automatically, you just need to declare them...
If some entity has no business logic and you just need to store it, so you don't need a "full model" for it. And that's what you're doing.
Hope I convinced you...

MVC: Model, Controller or Library?

I am building a CRM using a framework (codeigniter) for the first time and I am having trouble determining where a certain module should go while maintaining the MVC methodology. The module automatically generates a new user (when a new company is created) and emails the log in details out to the supplied email address.
I am familiar with the idea of skinny controllers and fat models but to compile all the information needed the module must request data from several different tables as well as inserting data into several tables.
The scenarios I have considered so far:
The logic is in the model where most of the information comes from.
Create a totally new model that deals with just this module and the multiple tables required.
Place the logic in the controller that deals with creating a company.
Create a new library or helper and call the module when it is needed.
Skinny controllers and fat models seem to suggest that one or two are the right options but I was lead to believe that a model should only deal with one table in the database.
What is the right approach to ensure adherence with MVC?
Codeigniter allows you to be flexible with your MVC approach. So the answer is which option is:
Easiest for you (or your team) to understand
Easiest to code maintain
Easiest for someone else to understand
There's no point putting your code into a library, if you dont have any other libraries and dont understand libraries. Same as if all your models are "fat", but only point to one table, do you want this model to be the only one that also points to 4 other tables?
Personally, if this "logic" only ever happens in one place, then I would place it into the controller, and call the 4x models you need to do each bit of the code.
If this "logic" occurs in multiple places, I would place it into a library and call it when needed.

Fetching object realted objects in domain model

How to take care of fetching related objects of one object?
For example object project has some tags. How and when should I fetch those object? In user initialization in mapper? That would be a big overload. The best way would be to load them dynamicly when system ask user for tags, but how to do that if model does not know anything about the mapper? Or just use Doctrine and forgot all about those problems?
Im asking this in relation to PHP Zend Framework. But any technology would suffice I think for this problem.
It is difficult to answer your question because you are not referring to a specific ORM or framework. If you are looking for suggestions, I would recommend using Doctrine as the model API and Zend Framework as a stand-alone library.
If you need a full featured framework you can take a look at any of these:
Symfony2
CakePHP
Zend Framework (as a framework vs stand-alone lib)
CodeIgniter
If you choose to go with Doctrine as your ORM, you can setup the schema file to ensure objects are relationship aware, then you can make references like:
// Joins tags table by way of intermediary object_tag table providing
// a M:1, 1:M relationship
$tags = $object->getTags();
Doctrine (1.2 not sure about 2.x) does employ lazy loading pattern, where the objects are only queried when requested.

Designing a Zend model for multiple tables together?

i am using Zend Framework to build a web interface for setting up ACL - permission rights - for users of a custom CMS. Since the ACL data is spread in 5 tables(users, groups, permissions, urls=action+controller, nice permission name for the user to understand) and i have only one controller with the four basic CRUD(create, list, update, delete) operations i was wondering what is the best way to do it?
All the examples in my books i've seen that each model extend Zend_Db_Table_Abstract and thus represents one table.
I was thinking i have to do a model that doesn't extend zend_db_table_abstract and then write the queries that i need by hand thus limiting myself to mysql database only?
p.s. please do not argue over the acl database structure
thank you
The definition of the Table Data Gateway pattern is
An object that acts as a Gateway to a database table. One instance handles all the rows in the table.
That's why you won't see it used any differently in Zend Framework. It's a Data Source Architectural Patterns while the thing you are asking about is a Domain specific class.
What you are encountering is Impedance Mismatch, meaning your Business Objects dont match the structure of your Database Design. The common solution is to use a DataMapper or an ORM to handle that for you.
The other solution would be to create a View in your database that joins the tables in a way that maps 1:1 to your required business objects. Then add a Zend_Db_Table for that view. You'd still have to come up with custom create, update, delete logic though. That's not data mapping though, but if you don't have any Business/Domain classes to map to, it's fine.

Categories