Using Yii 1 and ActiveRecord -
I'm pretty new in both.
$types = CasinoSgTypes::model();
$types->findAll();
$types->findByPk(3);
I thought Yii AR will try to search in recently received data first instead of that I've got 2 calls to the database. Probably I'm using it in a wrong way?
sure i can walk through the array of results received by the first query (findAll) manually, but I'd like to do this by means of AR.
in other words is there a way to force AR search inside already received data and only then ask the database or smth like this. how to use AR+Yii models in a right way to avoid unnecessary queries ?
When you call $types->findAll(); the data isn't saved in the AR class and as such you cannot search using it. An easier alternative to searching in the data is to use query caching:
$types->cache(3600)->findByPk(3);
Related
I just started learning laravel.
I don't know how can I fetch data inside a controller,
I want to use that data inside a controller to make more rules.
//Get Data From Database
$db = DonorDetail::where('donation_id', $paytmResponse['ORDERID'])->get();
$name = $db['donor_name'];
$email_id = $db['email_id'];
DonorDetail is my model to connect to my database.
I tried this code, but getting undefined error.
I'm trying to select only 1 row using the unique donation_id and use data of other columns of data inside my controller.
get returns a collection. You should use first to get the first result from your query:
DonorDetail::where('donation_id', $paytmResponse['ORDERID'])->first();
If no results were found, null will be returned, so you should also pay attention to that.
You only provided us with a small portion of your code, but it seems like you're doing a lot of things in a non-Laravel way. It's only natural because, as you mentioned, you're just starting with Laravel. My recommendation to you is to check out Laracast's free "Laravel From Scratch" course:
https://laracasts.com/series/laravel-6-from-scratch
It's by far the best resource for developers taking their first steps in Laravel (and it also has good resources for experienced developers).
Good luck!
you can use dd() to see the data.
dd($db);
However, If you want to fetch only first matching data, use following
$db = DonorDetail::where('donation_id', $paytmResponse['ORDERID'])->first();
$name = $db->donor_name;
$email_id = $db->email_id;
I am looking at using MongoDB with CodeIgniter, however my concern is how data is inserted into the database, most examples take the post values directly into a collection which is a dream because it removes an extract step... however a user could easily inject/overwrite values going into the database, compared to SQL where you would map one-one fields in the database, there appears to be no examples of how one would avoid this type of data injection...
Potentially I see two problems, namely additional values being injected and fields containing incorrect datatypes, ie: a name containing an array or object.
Is the solution to build model classes to map my POST data to along with datatypes or is there an easier method?
EXAMPLE:
MongoDB and CodeIgniter
Looking around I guess the only solution would be to map it into a local array or model class.
An example from: http://www.php.net/manual/en/mongo.tutorial.php
would be more like:
$post = $this->input->post();
$document = array( "title" => (string)$post['title'], "online" => (bool)$post['online']);
$collection->insert($document);
What does everyone think?
CodeIgniter has full active record abilities to help you deal with validation and sanitation of data: http://ellislab.com/codeigniter/user-guide/database/active_record.html
However you can also use something like Doctrine 2: http://docs.doctrine-project.org/en/2.0.x/cookbook/integrating-with-codeigniter.html to sovle this which has a fully fitted MongoDB verfsion of itelf.
Always confused about form resubmission . I know Header and session are the right way. But I don't know how can I proper use in codeigniter. Suppose
For single insert and update query
$this->db->query(' insert/up query');
$this->session->set_flashdata('success_message','successfully inserted');
redirect('my_contoller/home/index');
For pass $data array
$data['pass_data']="some array elements";
$this->session->set_flashdata('pass_data_from_flash_data',$data['pass_data']);
redirect('my_contoller/home/index',$data);
If above technique are right how can I pass query's data for retrieving. Suppose I have a query which return many data. Such as
$query = $this->db->query(" a query which return large data");
$data['return_large_result']=$query->result_array();
I just confused to using set_flashdata function. Is the right way?
The above method mentioned you are using is a valid web development design pattern. Codeigniter is a bit messy for this method but essentially yes it is the right way within Codeigniter.
Other frameworks such as Laravel support this feature better, allowing you to access old input via Input::old() amongst other methods.
What's a good way to pass a list of items 2-3 items to a method in my controller?
I was thinking of just using the URL.... like so:
http://myserver/myapp/mycontroller/mymethod/parm1/parm2/listitem1/listitem2/listitem3
Is there a better way to do this? This data is not coming from a form, but rather from a database query and I'm building a hyperlink with it.
I guess the only part that bothers me is that I won't know in advance how many items I have when i'm parsing this url.
Its possible that I'll get none, or all 3 or some value in between. So the method that then has to parse this url will just keep looping until uri->segment() returns false, indicating that it's hit the first empty uri segment.
Any suggestions would be appreciated.
Thanks
EDIT 1:
Just in case it wasn't clear, my model is getting the data from the database and will also build the list.
The question is really about parsing an undetermined number of uri segments.
Just wondering if there's a better way to do this.
Thanks!
EDIT 2
Here's some more information to help you understand my MVC app. I don't think my issue is the way I've organized my code as far as who is doing what.. But just in case it helps...
I have methodA in my model that queries database and passes back to my controller listitem1, listitem2 and listitem2.
The controller then builds a string that represents a URL like:
http://myserver/myapp/mycontroller/methodB/parm1/parm2/listitem1/listitem2/listitem3
Then the view display a hyperlink using the url above.
When the user clicks on this hyperlink, it calls methodB.
In methodB, I since I don't know the number of items, I will just loop through all segments until I hit my first false.
As far as why I need to do this / what I'm doing... here's some background info:
I'm query a database for a list of ports on a switch that are considered trunks - ones that should not be modified.
this is what method A does.
methodB run a command against a switch and it returns a bunch of data back. the view that displays the data from methodB will allow the end user to make further changes to the switch. before I display the data from methodB, i want to filter out the list of ports I got from methodA so they cannot be tampered with.
Hope this helps.
Edit 3
I need both methodA and methodB because they serve two different purposes. methodA displays summary data about ports from my database. Think of methodA as a function that shows documentation about the switch. The view for methodA in turn, provides "live" links to communicate with the actual switch - this is where methodB comes in. methodB is triggered by one of those live links and it goes and gets a list of ports - similar to methodA - except that it represents what actual, and it doesn't include user defined information about the port.
I guess I can have methodB communicate with my database and filter its data before it displays, but if i want to treat these two functions as separate APIs... aka - one set of functions get data out of the database, the other set is a tool to communicate with switches... - then i don't think i want one talking directly to the other. I would like the GUI to tie them together. In fact, i have created two separate models and controllers for what I'll call the database interface, and then the switch interface.
So far, i think the forms idea is the most elegant solution.
Thanks everyone, for reading.
place number of listitems as parametr 3
../mymethod/parm1/parm2/numberofitems/listitem1/listitem2/listitem3
and put 1, 2, or 3 as needed. In case when 0 put nothing - null, however make sure that controller would know what to do if null happend - do not expect items.
If the data is coming from a query it should be within a model in CodeIgniter if you wish for your application to truly MVC compliant. This might mean a restructuring of your application, which may be difficult but it would really benefit you in the future to create a model for all your database queries.
You can read up on codeigniter models here:
http://codeigniter.com/user_guide/general/models.html
And you can read up on the database class here: http://codeigniter.com/user_guide/database/index.html
I really suggest you do this.
If your data is already coming from a model you can call it by including the model:
$this->load->model('model_name');
$response = $this->model_name->model_function(parameters);
Edit: This would also solve the issue of an unknown number of list items as you can simply parse the response returned from the model function instead of trying to figure out a uri hack.
After reading all of the other answers + edits over, that's definitely not the way you want to do it.
Unless I'm misunderstanding your comments, here's the issue: The list of ports is domain data stored on your server. So why then, are you going to pull that data out, send it to the presentation layer, and show it to the user who will send it right back to the application? Skip the middle-man and have "MethodB" get that data.
Your "MethodB" should get this information itself before processing what it needs to do - domain data stays in the domain layer, and the view never sees any of that information directly (the user would see a link directly to "MethodB")
Alternatively, you could do this all in one query if your DB schema is conducive to such a join.
Is there a best practice in getting data from multiple database tables using Zend? I would like to know rather than end up wanting to refactor the code I write in the near future. I was reading the Zend documentation and it said that:
"You can not specify columns from a
JOINed tabled to be returned in a
row/rowset. Doing so will trigger a
PHP error. This was done to ensure
the integrity of the Zend_Db_Table is
retained. i.e. A Zend_Db_Table_Row
should only reference columns derived
from its parent table."
I assume I therefore need to use multiple models -- is that correct? If, for example, I want to get out all orders for a particular user id where the date is in between two dates what would I do?
I know that it would be possible to access the two different models from a controller and then combine their respective data in the action but I would not feel happy doing this since I have been reading survivethedeepend.com and it tells me that I shouldn't do this...
Where, why, and how? :)
Thanks!
If you're reading ZFSTDE, in chapter 9 (http://www.survivethedeepend.com/zendframeworkbook/en/1.0/implementing.the.domain.model.entries.and.authors) this problem is addressed by using a data mapper.
Also, you can join 2 tables, just be sure to first call on the select object the setIntegrityCheck(false) method. The docs say that a row should reference a parent table, doesn't mean it can not :)
Stop thinking about Zend_Db_Table as your "model".
You should write your own, rich, domain-centric model classes to sit between your controllers (and views), and your persistence logic (anything that uses Zend_Db/Zend_Db_Table/Zend_Db_Select) to load/store data from the database.
Sure, you can query several db tables at the same time. Take a look at the official ZF docs here http://framework.zend.com/manual/en/zend.db.select.html#zend.db.select.building.join
As for your example with getting all orders of a single user, table relationships are the answer http://framework.zend.com/manual/en/zend.db.table.relationships.html