-
azzuwan blizzx posted...
Good job.! your tutorials are excellent.
-
Shawn McCool posted...
You'll need to use something like pastie.org or something to post code. Doesn't work well on Disqus.
-
Shawn McCool posted...
The comment model should belong_to the post. The post model should "has_many" comments. In this way if you have a comment loaded you can do $comment->post to access the post, or if you have a post loaded you can use $post->comments to access an array of comments tied to this post.
-
Shawn McCool posted...
Please correct me if I'm wrong but you mentioned that your class name is 'fs' and you're trying to call Fstbl::all();
Your class name should be Fs (capital first) and the call should be Fs::all();
-
Shawn McCool posted...
It could be one of 2 things. Inside your Filesystem class declaration you may need to add:
static $table_name = 'filesystem';
Because it could be trying to look in the 'filesystems' table (which follows conventions).
Another possibility is that 'filesystem' is conflicting with a php keyword or a class that already exists.
-
Shawn McCool posted...
Ah, great! Sorry that I didn't respond sooner but my whole family was sick for a good while. I'm glad that you got it working.
A lot of people suggest that short tags are a bad idea. If you MUST work in an environment that you have absolutely no control over AND you are absolutely against having CodeIgniter automatically replace short tags (in config.php) then I suppose one shouldn't use them. But, these are incredibly fringe cases and probably represent the absolute worst kind of development work that someone could possibly be force to do.
The day that I'm forced to stop using short tags is the day I completely stop using PHP. =)
-
Santa posted...
Awesome Shwan! You rock!
-
Santa posted...
Hi Shawn,
Just wondering, how can I do the N+1 loading over multiple tables. The schema shown here: http://img534.imageshack.us/img534/332/useraddresqualification.png
$this->view_data['users'] = User::find('all',array('include' => 'addresses','include' => 'qualifications'));
Say, in the view, I want to print out all the user's addresses first and the user's qualification next?
Thanks.
Santa
-
Mo posted...
Hey Shawn,
I really appreciate these tutorials. They've helped me understand the codeigniter framework + active record. Thank You Again!!! I look forward to the next one.
-
Marhnix posted...
http://www.phpactiverecord.org/projects/main/wiki/Conventions
is a good way to go ;)
-
Marhnix posted...
Your table name have to be the same as your controller but small case and plural
Model : Post
Table : posts
Or you could specify the table name in the Model :
class Post extends ActiveRecord\Model
{
//explicit table name since your table is not "posts"
static $table_name = 'blog_post';
}
-
Marhnix posted...
Sorry i made a mistake, table name and Model name (not controller).
Your controller must have a different name !
Model : Fs
Table : fss (or another name if you specify it in the model)
Controller : Filesystem
Because if your controller is named 'Fs', codeigniter doesn't recognize which one your calling with Fs::all().
I had a similar problem with 'Event Model', 'events Table' and 'event controller'
Just rename the controller ;)
Hope it will works.
-
Marhnix posted...
Great tutorial. So you explain the benefits of adding array('include'=>'posts') in the query, but you put it in the by_author() method, not in the all_posts_by_author(). I suppose the right code is
function all_posts_by_author()
{
$this->view_data['authors'] = User::all(array('include' => 'posts'));
}
Am i right ?
-
Jimmy Pettersson posted...
Great tutorial as always, looking forward to the next one!
-
Eickhorst posted...
Nice tutorial!
-
Eickhorst posted...
I got an association that basically looks something like this:
Blog 1:n Post 1:n Comment
How would you get a Comment's Post now? I built a function in the Comment model that looks like this:
function get_mypost()
{
return Post::find_by_comment($this->id);
}
First I am wondering whether this is the way to do it and then I am not sure how the framework handles it...does it create a new instance of the Post object and if yes, how does this affect performance with a lot of data?
And one step further: How would you get a Comment's Blog?
function get_myblog()
{
return Blog::find_by_post(Post::find_by_comment($this->id));
}
Is this the way to do it?
Thanks and keep up the good work!
Eickhorst
-
Debow posted...
Wow I had things all jacked up and confused :) Thanks so much for the help. Things are working now. One last question though. How does CI/AR know which table to pull from?
This is the only line in my controller.
$this->view_data['commands'] = Fsystem::all();
should 'commands' be the table name?
-
Debow posted...
Thanks Marhinx, I changed my tablename to "fss" as well as adding the static $table_name='fss';.
I still get the Fatal error: Call to undefined method Fs::all()
-
Debow posted...
Sorry, this shows how new I am to CI and AR. I thought the Fstbl::all() should be ::all();. I changed that to Fs:all(); still no go.
Files
Controller = fs.php
Mondel= Fs.php
View=index.php
Table= fstbl
controller=>fs.php
view_data['commands'] = Fs::all();
}
mondel=>Fs.php
index.php
For now its blank doesn't matter what I put here I get the below error after changing my class name from Fstbl to Fs.
Different error now after changing the class name.
Fatal error: Call to undefined method Fs::all() in C:\wamp\www\
Thanks for the help.
-
Debow posted...
Sorry guys for the spam of questions but here's another one I don't see whats wrong.
I'm getting 2 errors on my view page.
1) Undefined variable: command
2) Trying to get property of non-object
my controller
fs.php
function index()
{
//echo '
'; var_dump(Fsystem::all()); exit; Below is the output from this dump which I see the data
$this->view_data['commands'] = Fsystem::all();
}
my model
Fsystem.php
-
=$command->description;?>
There are currently no commands.
Output from the var_dump
array
0 =>
object(Fsystem)[39]
public 'errors' => null
private 'attributes' (ActiveRecord\Model) =>
array
'id' => int 1
'user_id' => int 1
'command' => string 'df -gt ' (length=7)
'description' => string 'List all filesystems' (length=20)
'comments' => null
'ostype' => string 'AIX' (length=3)
'created' => null
'createdby' => string 'debow' (length=12)
'updatetime' =>
object(ActiveRecord\DateTime)[40]
...
'updateby' => null
private '__dirty' (ActiveRecord\Model) =>
array
empty
private '__readonly' (ActiveRecord\Model) => boolean false
private '__relationships' (ActiveRecord\Model) =>
array
empty
private '__new_record' (ActiveRecord\Model) => boolean false
-
Debow posted...
Shawn I finally figured it out. After enabling 'short open tags' in php it worked. Sorry for all the spam emails :)
-
Debow posted...
I've changed my table(fstbl) name and controller/model names to just (fs). Added this to my controller and still get this error. Did I add the static table_name to the wrong place?
view_data['commands'] = Fstbl::all();
}
Fatal error: Class 'Fstbl' not found in C:\wamp\www\dsp\app_senotes\controllers\fs.php on line 10
-
Debow posted...
Hey Shawn, have you had a chance to look at the code I pasted to pastie? If not no worries I know your busy. Thanks.
-
Debow posted...
Hey Shawn I changed my table name to fstbl and my controller/model to fs. I've add the following to my controller and no go. Did I add it to the wrong place? I've also checked and I have the correct MyLoader.php
function index()
{
static $table_name = 'fstbl';
$this->view_data['commands'] = Fstbl::all();
}
Fatal error: Class 'Fstbl' not found in C:\wamp\www\dsp\app_senotes\controllers\fs.php on line
-
Debow posted...
Hello Shawn, do you happen to have any insight on what the issue is with the my last post. I pretty much have everything setup exactly like your example with the first section of your blog post. I'm not seeing whats wrong or why I'm getting the above error.
1) Undefined variable: command
2) Trying to get property of non-object
Thanks
-
Debow posted...
Hello Shawn, do you happen to have any insight on what the issue is with the above post. I pretty much have everything setup exactly like your example with the first section of your blog post. I'm not seeing whats wrong or why I'm getting the above error.
1) Undefined variable: command
2) Trying to get property of non-object
Thanks
Here's the like to the code.
http://pastie.org/2826782
-
Debow posted...
Hello Shawn, I'm getting the following error and not sure why.
Fatal error: Call to undefined method Filesystem::all() in C:\wamp\www\dsp\app_senotes\controllers\filesystem.php on line 7
My controller is below.
view_data['commands'] = Filesystem::all();
}
This is my model