I want to create my own class in an existing CodeIgniter project.
I will create an object (say Object1) from that class in a controller (based on the user ID). This object will retrieve data in DB to populate its variables.
I want to be able to use this object in any function of my controller, and if possible I want to transmit it to my views.
In a near future, I might add another object (say Object2) that would contain several Object1.
What's the proper way to do it ? I started to write it in a model but it seems unapropriate ..
In case anyone's interested, I ended up using a normal php file that i store in /classes/.
Related
I'm currently saving an object into a class in Parse which currently works fine.
Up to now this has mostly been flat data or the odd pointer which i'm comfortable with.
However, I've recently created a relation column is linked to a Child class which consists of a String and 2 pointers. This data will be provided at the same time as the main row is created.
In theory:
Post data from form to php to process - before saving the main object, create a new ParseObject for the relation providing the String data and associativeArray pointer id's.
Is this the correct way to do it? I have a strange feeling that i will need to save the main row before adding the relation?
Any help would be appreciated greatly :)
You will need to save the main row before adding the relation, because the relation relies on a reference, which relies on an objectId, which doesn't exist until after the initial save. I'm unfamiliar with PHP, though would recommend you add this relation in the success handler of the save call, if there's an equivalent. If there isn't an equivalent, it may be better to write a cloud code function in js to do this, so you can utilize promises / callbacks, and pass the data you have to that function instead of saving the object on your PHP client.
I am building a PHP Laravel v5.3 app that will function as a Notebook with Notes and Tags as well as Note revisions/versions which can be used to restore a Note record to an older version of the Note.
It is basically an EverNote Clone in PHP similar to the open source Project Paperwork but will not be using AngularJS.
Based on that description above, my project has these Models:
Notebook
Note
NoteVersion
NoteTag
I have these Controller Classes to process the incoming HTTP Requests to the server Notebooks, Notes, and NoteVersions.
I need to have a function that will query the database for 2 NoteVersion records and return them as part of a JSON response which will be used as part of a view to show a comparison of 2 Note Versdions so a user can see the difference between the 2 records.
So assuming I add a new Controller Method to build this view, if I don't want the COntroller Method to handle the whole process of query the Database and doing all the work, what type of File would I put a function like that in instead?
Assume the function I want to build is like this:
get2NoteVersionsForComparison($currentNoteId, $oldNoteId);
In my controller method I want to call this method get2NoteVersionsForComparison($currentNoteId, $oldNoteId) and have it return a JSON response.
So where best would my get2NoteVersionsForComparison($currentNoteId, $oldNoteId) method live at?
A Repository, Model, ServioceProvider?
I like to create a new project or company-specific folder and create a class in that folder. So, for example, App\Acme\MyClassFile.php would be the location. Create classes that do everything else. Sort of like a repository, if you will. But where doesn't matter as long as you use proper namespacing.
Background:
I have an admin system, that needs to connect to and edit multiple databases. The database structures are 100% similar to each other, but the data varies.
What I have tried:
I'v tried to use $this->Model->setDataSource('db_variable_here'); in the controllers to change the database on the fly. The problem with this, is that all the related data seems to still be from my default database.
Example:
Imagine this: User HABTM Post, if I want to get a post from a different database, and use $this->Post->setDataSource('db_variable_here'); to achieve this, then it seems that I still get the related user from my default database, and not the same as the one I got the post from.
I'm guessing this is due to the fact that I only change the database on the Model Post, so it could be fixed by doing $this->Model->setDataSource('db_variable_here'); for each related model.
My Question:
Is it possible to change the datasource for every model in the app on the fly?
Ex. something like: $this->setDatasource('datasource_name')? Or do I really have to do it manually for all the related models?
Just save the database that you need to use in Session/Cookie (whatever tickles your fancy), then in your AppModel's __constructor() if the Session variable is defined then override either setDataSource() or setSource() accordingly.
Note that IIRC Cake's Session/Cookie are not available on the Models by default (because it's not supposed to be), so you might wanna use the good ol' $_SESSION or $_COOKIE or you will need to load it with App.
I do this to select to either use a Azure SQL database or a Rackspace MySQL database depending on the domain/URL, works as expected.
You could try making your own getDataSouce method in the AppModel.
You can see the CakePHP one here:
https://github.com/cakephp/cakephp/blob/master/lib/Cake/Model/ConnectionManager.php
So, in your AppModel, just make sure to accept/return the da
class AppModel extends Model {
//...
public function getDataSource() {
// some logic here to determine which source you want
// Maybe use Configure::write('MYSOURCE', 'other_datasource');
// somewhere else, then just check it here.
$source = 'default';
$this->setDataSource($source);
return parent::getDataSource();
}
//...
}
This should get called instead of CakePHP's 'getDataSource()', then it will run your checks to determine which connection to use, then calls CakePHP's 'getDataSouce()' to do the rest of the actual work of getting the data source.
Assuming you set a variable (like a Configure variable) that's accessible from here, you could set it once anywhere in the app, and when this runs, it will use whatever you've specified.
I have searched and searched and found tons of examples but it seems that everyone has a different opinion about how and when to use session, some use it some say it is evil...
Here is my use case.
I have a Class that has several variables that will need to be used on every page in my application. These variables values are set by making a SOAP call to an API that I am working with. The SOAP call is relatively quick but I am trying to understand how to avoid making a call to the API on every page. I would much prefer to make the call once and then "store" the values somewhere.
I would think that I would just create an Instance of my class on some say Init.php page, make the SOAP calls and then store the whole class in session. Then on all of my pages include the Init.php page. In that page I would do a check to see if the Class existed in the session and if so then pull it form the session.
I know I have to serialize\deserialize the class to do this but I am looking for some feedback here on weather this is the right way to satisfy this use case or if there is a better option?
I am kinda new to PHP, mostly a .NET guys and in .NET the session is generally the best way forward.
All input is appreciated.
thanks
I assume when you stay "serialize\deserialize the class," you really mean you want to serialize/deserialize a class instance (an object) in the session, not the actual class definition. Be careful when using the terms class, instance, and object, since they are not interchangeable and can lead to confusion.
An object can be easily stored in a PHP session. PHP automatically serializes the object at the end of the request and deserializes it when the session data is read on the next request.
session_start();
if (!isset($_SESSION['soap'])) {
$_SESSION['soap'] = doSoapRequest(); // Returns an instance of your class.
}
When an object is serialized, only the variables defined in the class are saved along with the name of the class. When it is unserialized, the class definition must be available (that is, either autoloader or explicitly included into the script). Unserializing will create a class instance with the same data as the object that was previously serialized.
I am new to Symfony2 and I am not sure where I should save a class that updated multiple tables(entities).
From reading documentation and tutorials it says I should not put any other tables reference within the entity class; I could put it within the controller class, but again many people have said this class should be as simple as possible and not include business logic; Not in repositories, because these are used for query data and not for update or inserting.
Is there a standard folder structure where another type of class for working with multiple entities(tables) should be saved? Should the business logic really be stored in the controller classes?
Symfony2 is very flexible in this regard.
You're right, entities are for one "table" only.
I would suggest you look into Services, as they are a good way to move your code from a controller to a separate class. You basically call your service and use the functions it provides. This will slim your controller down.