Welcome Guest, Not a member yet? Register   Sign In
Passing Objects As Function Parameters
#11

[eluser]danmontgomery[/eluser]
If it's a CI model you can load it with the load library

Code:
$this->load->model("Blog");

If it's not, and it's just a normal class, you need to include it with php's native include functionality: include(), include_once(), require(), or require_once().

Each time you call include() or require(), the file will be included (require() will throw a fatal error if the file can't be included, include() will only generate a warning). include_once() and require_once() will only include the file once, and return false any time it's included after that.

Functions and classes defined in included files have global scope, so they only need to be included once.

http://php.net/manual/en/function.include.php
#12

[eluser]Shouvik-S-Mazumdar[/eluser]
SOLVED Thanks..

the error was with my IDE ( PHP Designer) it had recognized the class so i assumed we would not need to add a reference to it ..

added include "DS/blog.php"

in the constructor of the model.

Now i can almost imitate the generic list concept

in my controller i do something like this



foreach($this->MYBLOG->getBlogList() as $blog)
{

print "TITLE :".$blog->title;


}


So far..it works like a charm Smile


thanks...for the help
#13

[eluser]danmontgomery[/eluser]
I'd suggest you read up on models... What you're describing is already built into CI, and you're circumventing that functionality and replacing it with your own.

http://ellislab.com/codeigniter/user-gui...odels.html
#14

[eluser]Shouvik-S-Mazumdar[/eluser]
Yes i know about models , but wanted another level of abstraction. Actually i wanted to pass an object of blog from the controller

So suppose i have Data structure class :

class Blog
{

var blog_name;

}



I create a model for the blog operations


class BlogModel extends Model
{

public function()
{
parent :: Model();
include "blog.php"; // contains the variables


}

function InsertBlog($blog)
{
// insert into database $blog->blog_name;

}


}



Now in the controller

i just load the model // it auto loads the datastructure. fill the BLOG object and pass it to the insert

so the hierarchy is like the Model class contains only the operation and the data structure defines the data vars i need. ofcourse i can still copy paste the same vars in the model file .. but i had two concerns :

1) Will i have that flexibility of passing a $blog object to the function Insert i just did with a Data struct file . So i would need

2) is there any performance difference between the two methods


can we discuss on this ?
#15

[eluser]danmontgomery[/eluser]
The built in models do not directly interact with the database in the way you're describing, you have to load the DB library and interact with it that way. But there are plugins (Datamapper, for example), which do what I think you're looking for:

Code:
// Create new User
$u = new User();

// Enter values into required fields
$u->username = "foo";
$u->password = "bar";
$u->email = "[email protected]";

// Save new user
$u->save();

http://stensi.com/datamapper/pages/toc.html




Theme © iAndrew 2016 - Forum software by © MyBB