Extending the CI_DB_active_record class in codeigniter 2.0 - codeigniter

I am writing my first program with Codeigniter, and have run into a problem. I will start with a focused description of the problem and can broaden it if I need to:
I need to write a multi-dimensional array to the DB and want to use the insert_batch function from the CI_DB_active_record class to do so. The problem is that I need to write empty values as NULL for some fields while other fields need to be empty strings. The current function wraps all values with single quotes, and I cannot find a way to write null values to the database for specified fields.
I would also like to increase the number of records per batch.
I see how to extend models, libraries, etc., but is there a way to extend the CI_DB_active_record class without modifying core classes? The minimal amount of core class modification to make this work that I have found is modifying the following lines in the DB.php file (changing the require_once file to the new file that extends the CI_DB_active_record class and changing the CI_DB_active_record class name to the new class name):
require_once(BASEPATH.'database/DB_active_rec'.EXT);
if ( ! class_exists('CI_DB'))
{
eval('class CI_DB extends CI_DB_active_record { }');
}
Can I do better?

There is no out-of-the-box solution to do this. Your hack is a reasonable solution and if it does the job then spot on.
If you have any changes to make to the core (plenty of room for improvement in some parts of AR) then why not hop on BitBucket and help out with Reactor?
https://bitbucket.org/ellislab/codeigniter-reactor

Related

Customizable model file for a Laravel 5 package

I'm developing a Laravel 5 package where I have a "Member" model which currently extends App\User model. I would like to know the best practice to let any developer use a custom "Member" model instead of the one from the package. This is for example to allow a developer use another table.
One approach that seems to work without having done a deep test with it is to make an alias in my package service provider in the register() method:
$MemberModel = 'MyVendor\MyPackage\Member';
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('MyMember', $MemberModel);
In this case I have to:
Replace all entries in the code from the original class to the alias
Delete all php "use" entries related to it
Get the value of $MemberModel from a config file or the database
But I don't know if it is a good way to solve it or It may cause any conflict.
Is there any other and better approach for this goal? Thanks in advance!
I finally had to test by myself this approach without haven't read the solution anywhere else, but anyway everything seems to work fine in my source code.
If anyone is looking for doing anything similar, the code example in my question works because the $MemberModel is defined with a value. If you want to get that value from a Model instance, as me, you have to add that code in the boot() method of the service provider.

Where to put common logic in cakephp app

This is something I have struggled with for a while to get my head around
I have a shell which at the moment grabs data from a 3rd party, processes it and saves processed data into my db. The processing can save to related tables as well.
I also want to have some way of submitting data via chrome extension to be processed in the same way. The data is going to be in the same format in both scenarios so I was hoping to move the logic I have in my shell into something that both a shell and controller can use.
Part of my processing involves loading any currently saved data in some situations too - and I found that I can't use loadModel unless I am in a controller?
So where should this logic go, and how do I make sure that solution has access to all the part of the framework I need still?
You can create an Utility folder under src containing php files.
Here you can define your namespace of class and can instantiate easily.
You can use statements also.
--src
|--Utility
|--Example.php
<?php
namespace App\Utility;
class Example
{
function __construct() {
//constructor
}
function demo() {
//function for specific task
}
}

Joomla how can i create model from other component

I'm a beginner in joomla. I create own component and would like to use model from other component (exactly contentbuilder).
I find few different ways how to create model but my problem is that
class ContentbuilderModelEdit extends JModel
use JPATH_COMPONENT_ADMINISTRATOR in it. When i create model ContentbuilderModelEdit i get warnings in lines that using JPATH_COMPONENT_ADMINISTRATOR constant.
Is it possible to create model using that constant from other component?
Thanks for your answers
Alas no. The JPATH_COMPONENT and JPATH_COMPONENT_ADMINISTRATOR are defined constants, and cannot be changed.
Sometimes the developers do this instinctively (it's easy) without realizing the kind of limitation they're putting on other developers. You might consider contacting the developers and propose such a change; if they accept, you won; if they don't, write a sed script that performs the changes (replacing it with JPATH_SITE and JPATH_ADMINISTRATOR . '/components/com_contentbuilder', and apply it after each update.
Or, copy their model into your component and rename it if it supports it.
:)
To call a model from another component you need firstly to include the path of this model:
JModelLegacy::addIncludePath(JPATH_SITE . '/components/comp1/models', 'Comp1Model');
Secondly you have to create an instance of your model:
$model = JModelLegacy::getInstance('Model1', 'Comp1Model');
After that you should be able to use the methods of your model.
ref link

CodeIgniter - where to put functions / classes?

Am having problems understanding where classes should be kept in CI. I am building an application that describes / markets mobile phones.
I would like for all of my functions (i.e. getphone, getdetails etc.) to reside in one class called Mobile - I understand that this file should be called Mobile.php and reside in the controllers folder.
Can I then have multiple functions inside Mobile.php? E.g.
public function getphone() {
xxx
xx
xx
}
public function getdetails() {
xxx
xx
xx
}
Or do I need to put each function in its own class?
I'd really appreciate looking at some sample code that works. I've been going through the documentation and google for a few hours, and tried all sorts of variations in the URL to find a test class, but without much luck! I've even messed around with the routes and .htaccess...
All I am trying to achieve is the following:
http:///model/HTC-Desire/ to be re-routed to a function that accepts HTC-Desire as a parameter (as I need it for a DB lookup). The default controller works fine, but can't get anything to work thereafter.
Any ideas?
Thanks
Actually it works like this:
Controllers and Models go to their perspective folders as you know it
If you want to create functions that are not methods of an object, you must create a helper file. More info here :
http://codeigniter.com/user_guide/general/helpers.html
Now if you want to create your own datatypes (classes that don't extend Models and Controllers), you add them to the library folder. So if let's say you want to create a class "Car" you create this file:
class Car{
function __construct(){}
}
and save it in the libraries folder as car.php
To create an instance of the Car class you must do the following:
$this->load->library('car');
$my_car = new Car();
More information on libraries here:
http://codeigniter.com/user_guide/general/creating_libraries.html
Yes, you can have as many functions in a controller class as you'd like. They are accessible via the url /class/function.
You can catch parameters in the class functions, though it's not advisable.
class Mobile extends CI_Controller{
public function getPhone($phoneModel=''){
echo $phoneModel;
//echo $this->input->post('phoneModel');
}
}
http://site.com/mobile/getPhone/HTC-Rad theoretically would echo out "HTC-Rad". HOWEVER, special characters are not welcome in URL's in CI by default, so in this example you may be met with a 'Disallowed URI characters" error instead. You'd be better off passing the phone model (or any other parameters) via $_POST to the controller.
Classes can exist both as Controllers and Models, as CodeIgniter implements the MVC pattern. I recommend reading more about that to understand how your classes/functions/etc. can best be organized.
Off the top of my head, Pyro CMS is an application built with CodeIgniter and the source code is freely available. I'm sure there are others.
I think it's best you handle it from one perspective, that is; create a utility class with all your functions in it.
The answer to the question of where to put/place the class file is the "libraries" folder.
This is clearly stated in the documentation. Place your class in the libraries folder.
When we use the term “Libraries” we are normally referring to the
classes that are located in the libraries directory and described in
the Class Reference of this user guide.
You can read more on creating and using libraries Creating Libraries — CodeIgniter 3.1.10 documentation
After placing the newly created class in the libraries folder, to use just simply load the library within your controller as shown below:
$this->load->library('yourphpclassname');
If you wish to receive several arguments within you constructor you have to modify it to receive an argument which would be an array and you loading/initialization would then be slightly different as shown below:
$params = array('type' => 'large', 'color' => 'red');
$this->load->library('yourphpclassname', $params);
Then, to access any of the functions within the class simply do that as shown below:
$this->yourphpclassname->some_method();
I hope this answers your question if you have further question do leave a comment and I would do well to respond to them.

How to serve a MVC-view with image paths from directory?

I'm kind of new to the whole MVC concept, since I just recently started developing a web site using CakePHP framework. Therefore I turn to you asking for how to achieve the following in a best-practice-way.
I want to be able to place a number of pictures in a directory, which then is scanned for all filenames in it. These filenames should afterwards be passed to an arbitrary view, which then loops through all filenames making img tags of them.
It would be nice if this could be done in a general way, so that I could reuse the code for same task but with a different directory name.
I have already tried the following two approaches. Nevertheless, non of these felt like the best way to do it, for some reason.
Create a model with $useTable=false.
Create an ordinary class and import it as a vendor.
What is the best way to achieve what I describe above?
I'm thinking your original idea is the better one, use the Model to traverse/read/write the directory. Think of the File structure as your data source. You can pass the dirname to the model with $this->data and then let it use the File class to retrieve what you need. This would make it portable across Controllers (with loadModel())
Later on down the road if you move your image paths into a DB you only have to re-write the model to take that into account.
I've done the following before: create an e.g ImageManager class and pass it as an IImageManager to the constructor of your e.g ImageController instead of passing a repository. The ImageManager could then do all the work that you mentioned.
EDIT: I did that in .net mvc and don't have experience of CakePHP, but I guess there's a place where you can register the concrete implementation of IImageManager as ImageManager so the framework would know what to pass to the controller's constructor
I would create a helper for this purpose.
Edit: And for trawling through the filesystem use the Folder class from within your helper.

Resources