Yii save data to different tables - php

I am new to Yii. I need to save data collected from a single form to three different tables.
So my doubts are
How can I design the Model class (CformModel or CActiveRecord)??
How to design the view??
In Controller how can I save the data to different tables??
I need to manually validate some vales like md5 hash etc

you need to create three models. And use according model fields and save all three models.
In Yii one table - one model.

In your controller: saving your different models for different tables will look like:
$modelB=new Addresses;
$modelB->attributes=$sess['addresses'];
$modelB->save();
$modelC=new TenQs();
$modelC->attributes=$sess['tenqs'];
$modelC->save();
To render multiple models to one form you just keep listing the models in the render statement.
$this->render('create',array('modelB'=>$modelB,'modelC'=>$modelC));
That would work in your controller. This example assumed Active Record.

Related

yii framework : how $dataProvider get data and how it works?

I am learning YII framework and I am at a beginners level.
I created a CRUD option and I found the following script my view file
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
But I don't understand How $dataprovider got the data from data base, I didn't see any related function on the Model class, also how can I update the data is $dataprovide, ( I have to replace one foreign key with correct data ie family_id with family_name)
Yii Data providers (there are many, the easiest way is to use CActiveDataProvider) are just data containers that can be used, for example, to populate a grid (CGridView widget, for example) or
The controller is the part of your application that feeds data into your views. You probably have a line or two of code in your controller where you create this data provider so you can send it to your view.
You can create a Data Provider by yourself (documentation of class CActiveDataProvider), just by instantiating it, but if you used gii or some simlitar device to generate models, then your model class should have a search() method. This is a helper method created for you so that you don't have to instantiate the data provider yourself. (when this method is called, a new CActiveDataProvider is returned)
As I said, a data provider is just a container of models. In your case, (as you are creating a CListView), it probably contains just one Model (the model whose details you are trying to display). Data providers can also be used for grids and in that case one data provider will hold not one but many models (in general, one per grid row).
p.s.: If you need to change things like foreign keys and/or attribute names, you need to do it in your Model class (that is, your ActiveRecord), not in the data provider. The data provider - and any widgets you use - are only used for displaying purposes.

Koahan ORM: Adding extra data to many-to-many relationships

I have my models setup as a many-to-many using through with a pivot table. However, I would like to add some extra data into the pivot table.
In the past (kohana 3.0) I was able to provide extra data with the add method
$obj->add('alias', $related, array('extra'=>'data'))
But its seems in Kohana 3.3 that the add method does not provide the third parameter for extra data, and I cannot seem to find how to do this short of after saving, adding more data then re-saving.
This isn't supported anymore since Kohana 3.1.
The reason they removed it (Source: http://dev.kohanaframework.org/issues/3754):
We decided to remove this because it's better to use a through model
if you need to put data in your pivot table. Inserting the data
directly in the add() method bypasses validation and filtering that
would normally be in your model. Use a model if you need data in your
through table. We won't be changing this.
You now have to make a model for the pivot table and place the additional information in that model.
Then instead of using has_many "through" (n:n) you should use has_many (1:n) relationship for both tables to the pivot table.
I hope this answers your question.

Using multiple database tables in CakePHP

In my application, i have many methods that my controllers use commonly.
At first i copied them to controllers, then i found out that i must put them in AppController.
So i. reach those methods from derived controllers by "$this->commonmethod"
The methods that i put into AppController creates different types of data,
so i need to put them to 4-5 different tables in my database.
Most of the tables don't have a relation between each other.
But most of my controllers will use that tables to fetch related data for them.
(I checked the examples in cookbook, there are examples about Blogging,
category and tags. Where tables have relation between them)
Should i put my common controller code into a Plugin or Component? Or what would be the decision criteria?
Is it possible to use multiple database tables from a controller or a component?
Do Datasources or behaviours are suitable for this case.
Thank you in advance
It's hard to say what the best approach would be, given we don't know much about the database structure, what the controller methods do, and how they are used.
But one of your questions is very straightforward:
Is it possible to use multiple database tables from a controller or a component?
Yes, that is possible. Just create a model for each table (use public $useTable = 'tablename if cake cannot detect the table name automatically from your model names). Then, in AppController (considering your code will stay there), use this:
public $uses = array('List', 'all', 'models', 'you', 'need');
Best way is (if possible) to redesign the database, because in cakephp a good database design resolve half of your problems. If this is not possible then second one is use Components and using this component you can you multiple database tables.

Zend_Db_Table joined queries vs database view

I was wondering what would be the best practices and the best way to achieve consistency while accessing your database data :
Current structure goes as follow
Data Access --> Business Logic --> Controller --> View
My data access layer is composed of a Zend_Db_Table, Zend_Db_TableRowset and a Zend_Db_TableRow for each table.
My business logic is stored in the model named based on a the table
Example of problematic query :
I want to get a specific user based on his username. To do so I have a user table and a role table (role.id is referred as role_id in the user table).
I don't want to have to use the findDependentRowset that will run additionnal queries for each row that get returned. (will get problematic in data grid that display the data as many rows can be returned).
The choices I have (getName() is used in this example to simplify, but it can be any processing) :
Make a custom join on the role table in the user table which will return an index array composed of associative array. In that case I can't call my getName() function defined in my Model_DbTable_User to build the name (first name + middle + last name). Even if I "cast" my array to a Zend_Db_Table_Rowset (or my custom table_rowset) I can't access my custom class method since I get a generic Zend_Db_Table_Row object.
Make a custom join but build the name using CONCAT() in my query at runtime, I still get an array but the name is build so I don't need the getName() method. But if I have specific logic to apply i'm stuck.
Make a view joining user and role tables in my database and create a new set of Zend_DbTable, Zend_DbTableRowset and Zend_DbTableRow. This way I can have specific logic in my database stack.
An ORM (propel or doctrine (1 or 2)), I have no experience with these, I might need more informations to make the right choice.
My alternate goal is to be sure I get consistency in my data structures
ie:
array all the way :
array(
array(row1),
array(row2)
);
object all the way
$row = $rowset->current();
$row->field;
Creating a view should be done regardless because it complements the other ideas more than it competes with them. The view will:
Abstract the database to something that you can more easily work with.
Be faster because there's no parsing.
Be accessible outside of your application.
Once you've created the views, you can choose a strategy that best solves the problem. If you're creating a form that represents a single entity, then an ORM is probably a good fit. But if you're displaying large lists of data or generating reports that contain many entities then using a declarative language like SQL is probably easier and would perform better.

MVC good or bad practice

I am working on my first MVC application in PHP (CodeIgniter framework). Let's say I have 2 controllers: Cont1 and Cont2
Cont1 is associated with the MySQL table 'cont1' and Cont2 is associated with table 'cont2'
In my Cont2 model, every time I have to insert a new entry in my cont2 table I have to update a field in cont1 (they're relational tables).
Is it considered a bad practice if I execute a MySQL query in the model of Cont2 to update a table that is associated with the model of Cont1? Are there any good MVC approaches to this problem?
Controllers are not associated to tables, nor is your Model just the Database, nor is Code Igniter's definition of ActiveRecord correct. Apart from that it is okay to update whatever needs updating from within the Model.
I usually have one controller, a page controller, and another, an admin controller.
I then have a "news" library for example and that library calls functions from the "news" model.
The functions in here allow me to add news, delete it, edit it but it also allows me to pull news from the database and display it on the website.
So I can use that library in both my admin controller and page controller. So controllers aren't associated with actual database tables. My news model utilises several other libraries that upload images and data into other tables in the database and your method is as a result perfectly fine.

Categories