What I have is the following db structure(tables):
lists[name,id]
list_items[title,list_id,content]
I've created the needed files and code(the MVC) needed to manage the first table(lists).
I also added the hasMany to the model class. At that point I am stuck.
What I need is a solution for managing each item (basic CRUD, I assume that complex management is just an advanced CRUD that I will find out how to do by myself).
I will be specific: since it's a content that have no place (but the admin) that it will be used by itself, should I -
create a full mvc structure for it? (can or should I implement it somehow[how?] in the lists package?
if not, how can I attach the tables? (since the use is about to be dropped in version 2)
would an element(cake concept/context) will be the appropriate way to create a view for such situation?
ANY insight will be appreciated.
If I undertant correctly, you want to create a CRUD part of this tables by yourself, without bake.
You need to write all the MVC estrucure and be carefull with the naming combention of cakephp http://cakebaker.42dh.com/2006/02/18/cakephp-conventions/
You need the model into app/models and also a a controller into app/controllers (remember naming combentions) and for each model you need a folder into /app/views.
Alfo, every, every function in your controller needs a view, even if this action doesn´t write anything to screen
I hope this was usefull.
Have you tried using Cake's bake feature? Your CRUD will be automatically created in about 2 seconds. I would also recommend you do the Blog tutorial to get a feel for scaffolding.
CakePHP is all about convention over configuration. Eg naming conventions for tables, controllers, models etc.. So much can be done automagically.
Related
Is there any application which automatically creates html form from given mysql table like the one gii does of yii framework.
Or, is there any way, i can use gii for my own purpose without using using yii framework.
I don't want complete CRUD feature in it, Even if it's just list out my data with pagination, that will do my work.
Thanks
This URL will help you!
http://developer.ifreelance.asia
I found this on facebook page. I use it myself, and this is real handy.
Well, if you really think about it, phpmyadmin creates forms using the database structure. The ultimate CMS is phpmyadmin :).
Anyway to answer your question. You can always create your own Gii template that creates it as you need it. I have custom models and custom forms that get created. There is nothing that prevents me from rolling out anything I actually want with Gii.
The only 1 problem that I can think about is that you create the model and then, based on the Yii model you create the CRUD. If you really want to use Gii you should create your own model however you want, create a Yii model too (quite easy to create both, one after the other as you can use 2 different templates) then create the CRUD based on the Yii model, but the CRUD you need.
Probably you can do it directly but I have never tried it.
Point is, you can use Gii to create a model for any ORM. It just creates a text file and you can modify it. The CRUD might require 2 steps but you should also be able to create the CRUN as you want.
i'm new to Yii, still learning and loving it a lot. So the thing is that i have to build a product retrieval system, which is based on Amazon web services.
First i have created the necessary tables to hold the information about the products. Then i've created the model class using the awesome Gii. After that i generated the CRUD using Gii again. Now i'm kinda stuck. So Gii provides a form to let the users populate the tables with the necessary info. Now in my system/app i have no need for a form input, for any of the main tables that would hold the product information. The tables should auto populate with data gathered from Amazon API. Only a few tables can have form related to the input fields.
So can anyone please guide me in the right direction, on how would i start implementing the functionality. Should i remove this code from the corresponding view and write the functionality in the Controller class.
<div class="row">
<?php echo $form->labelEx($model,'type_id'); ?>
<?php echo $form->textField($model,'type_id'); ?>
<?php echo $form->error($model,'type_id'); ?>
</div>
Or should i generate a separate view files. Right now i can't seem to find any headway. How to start ? What should be the workflow sequence for a typical application built using Yii. Where would i put the business logic? Of course i know that the business logic should always reside within the Controller class as per the MVC paradigm. But should i write all of the application logic in a single controller class.
I've read about modules and components. But the dilemma i'm facing is that i don't know when is the right time to separate the necessary logic into its respective modules or components.
I'm already following the Web Application Developement with Yii and PHP 2nd Edition, and i admit that its a fantastic book. I've read it two times till now. But i'm getting stuck when i get down to build my projects. Just don't know where to start. My application would not follow a similar flow diagram like the book example.
I just want to adhere to the conventions that are set in Yii. I've heard in many places that once you get used to the conventions in Yii your productivity increases hundred folds. So what are the best practices ?
Say::
1) What are the conventions when building an automated/real time system ?
2) How to initialize the specific controller logic sequence
3) How to get the most benefit from the CRUD/Model/Module system already built by Gii ?
With the Gii code generation I try to delete anything that isn't being used. It makes maintenance harder if left in and you can always add it later if required.
Controllers do not need to be related to a table. If anything they should be related to an area of functionality (eg RestApiController or ReportController).
You should keep your controllers as thin as possible. It makes unit testing easier if appropriate logic is in the models. See the last paragraph of the Yii best practices documentation http://www.yiiframework.com/doc/guide/1.1/en/basics.best-practices
Personally I think the Gii CRUD generation is only good for admin level configuration. For example you need to know your database design/relationships to be able to create your data correctly. For a normal user it can be to complex and not very user friendly.
I've been considering adding a Service Layer to my Yii projects. This is where all your wiring up of business logic goes.
I'm using Codeigniter to flesh out a pretty large project (especially for a n00b). One issue I'm having is how to organise my files and methods. I've broken my project down into features - the app is a task management software so already we have basic features such as "Task", "Project", "User" etc.
The way I intend to do this is by creating controllers for each and then following CRUD methodology in each. So for example in Task we would have the following methods:
create()
read()
update()
delete()
This makes sense in my head. Now in terms of Views, should I have multiple views, or should I combine create and update into the same form? Also, where does non-View functionality go, such as setting cookies etc?
This is quite a specific question but if anybody has any more holistic guides on general structure convention for CodeIgniter projects I'd be very grateful.
I'd say you got it right. This is what I do.
I tend to use the same view for create and update, keep it DRY (don't repeat yourself) if you can.
Non-view related stuff that does not handle anything business-related goes in what I call helper-classes. If it's business related, I put all the logic into services, so I can unit-test them without being dependant of any framework (not sure how new you are at this, but oh well :) ).
You can also use Grocery Crud, a library that provides out of the box CRUD functionality for codeigniter.
It handles pretty good 1->n and n->n relationships so its convenient for small projects.
I don't know if you are familiar with it. If not give it a try. It will save you tons of time and effort.
My controller consists of these methods, which follows REST API guidelines:
read -> get all records.
find -> find record by primary key/id.
create -> show the form view.
store -> insert data from the form into the database.
edit -> show the form view, populated with current records' data.
update -> update data from the form into the database.
delete -> delete data from the database.
This is called Resourceful Controllers.
Yes, you can combine create and edit in same form. However, there are cases those require you to use different create and edit form. In that case, just make 2 separate forms.
And... like #Theodore suggested, GroceryCRUD is worth a try if you don't need too many customizations.
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.
As I delve a little deeper into Yii I'm now wondering if relying on Gii and Giix to generate my models and "admin" CRUD may be a crutch rather than a time-saving tool. Many times in the beginning stages of small projects it helps me get going more quickly, allowing me to focus on database design. However, whenever I make a change to my table structure or relations, I find myself having to rely on GiiX to re-generate the model. Before I do so, I always copy the parts of the model that I have written so that I can paste it into the updated model later. This seems like a tedious thing to do and I'm now wondering if it is saving me any actual time. I have a few questions:
For Yii users specifically, once you've been doing Yii for a while do you even bother with Gii or GiiX? Did you quit using it because it was no longer useful, or because it was a crutch? Did you work on writing your own code generation and scaffolding tools?
For all coders, do you feel code generation tools should be avoided when learning a new language or framework?
My hope is that there is an effective way to use Gii and other code generation tools even after updating table structure multiple times and writing in my own code, sans the copying and pasting and keeping track of what's what.
Please let me know your thoughts!
Gii is useful to generate the initial boilerplate code and directory structure.
As the project go ahead, I use the diffs provided by Gii to add the relevant new code snippets in my model class files. Say you modify a table. Go to Gii and try to generate the model. You will get notified that the model class file exists. Also, you will see the link that gives you the diff in a pop-up.
I don't know if it's possible with Yii but with another framework that I use we extend the model classes and put our custom code into those extended classes. In the app we only reference the extended class, not the base (generated) model classes.
Since we don't put any custom code into the base model classes they can be re-generated without worrying about overwriting any custom code.
However, whenever I make a change to my table structure or relations,
I find myself having to rely on GiiX to re-generate the model.
You really do not need that. Yii design makes all of your table fields available as attributes in your model. This way, if you add a new fieldX to your TableA, you can imediatelly use $modelA->fieldX. You do not need make any upgrade in your model. Yii 'knows' you have changed the table.
See:
"Although we never explicitly declare the title property in the Post
class, we can still access it in the above code. This is because title
is a column in the tbl_post table, and CActiveRecord makes it
accessible as a property with the help of the PHP __get() magic
method. An exception will be thrown if we attempt to access a
non-existing column in the same way."
Source: http://www.yiiframework.com/doc/guide/1.1/en/database.ar
For Yii users specifically, once you've been doing Yii for a while do you even bother with Gii or GiiX? Did you quit using it because it was no longer useful, or because it was a crutch? Did you work on writing your own code generation and scaffolding tools?
I use Gii in all of my projects for the most of models or CRUD generation. It is very useful. I can customize the generated code the way i want. I even have done some customizations to 'skeleton' of Gii generator so that the code generated to be in my language, not english, and with some methods/attributes I need more.
For all coders, do you feel code generation tools should be avoided when learning a new language or framework?
No, IMO. The generated code is one more way to learn.