<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Documentation</title>
	<atom:link href="http://lukebaker.org/projects/activerecord-in-php/documentation/feed/" rel="self" type="application/rss+xml" />
	<link>http://lukebaker.org</link>
	<description>lukebaker.org</description>
	<lastBuildDate>Thu, 26 Aug 2010 11:17:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Derek</title>
		<link>http://lukebaker.org/projects/activerecord-in-php/documentation/comment-page-1/#comment-23959</link>
		<dc:creator>Derek</dc:creator>
		<pubDate>Tue, 06 Jul 2010 21:01:57 +0000</pubDate>
		<guid isPermaLink="false">http://lukebaker.org/projects/activerecord-in-php/documentation/#comment-23959</guid>
		<description>Thank you Sebastian,

That count method did the trick for me.</description>
		<content:encoded><![CDATA[<p>Thank you Sebastian,</p>
<p>That count method did the trick for me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Arwin</title>
		<link>http://lukebaker.org/projects/activerecord-in-php/documentation/comment-page-1/#comment-23138</link>
		<dc:creator>Ben Arwin</dc:creator>
		<pubDate>Thu, 11 Mar 2010 20:29:05 +0000</pubDate>
		<guid isPermaLink="false">http://lukebaker.org/projects/activerecord-in-php/documentation/#comment-23138</guid>
		<description>I used Fyodor&#039;s solution for adding custom foreign_key and class_name attributes in the $belongs_to and $has_many arrays, but I had to modify it slightly to get it to work.

Instead of using empty() to test if $options[&#039;foreign_key&#039;] was set or not, I had to use isset().  E.g. if (!isset($options[&#039;foreign_key&#039;])) { ... }
So change all instances of &quot;empty&quot; with &quot;isset&quot; and you&#039;re good.

Hopefully this will help others who are banging their heads against the wall ;)

By the way, this is a great project!  This beats all of the other pre-PHP-5.3 ORM packages that I&#039;ve tried, hands down.  It&#039;s still going to be a while before everyone&#039;s supporting PHP 5.3, so IMHO it&#039;s worth it to make this a &quot;real&quot; project with releases, user forums, and a public git repo.  I&#039;d be happy to help if the author is interested?  

Cheers,
-Ben</description>
		<content:encoded><![CDATA[<p>I used Fyodor&#8217;s solution for adding custom foreign_key and class_name attributes in the $belongs_to and $has_many arrays, but I had to modify it slightly to get it to work.</p>
<p>Instead of using empty() to test if $options['foreign_key'] was set or not, I had to use isset().  E.g. if (!isset($options['foreign_key'])) { &#8230; }<br />
So change all instances of &#8220;empty&#8221; with &#8220;isset&#8221; and you&#8217;re good.</p>
<p>Hopefully this will help others who are banging their heads against the wall ;)</p>
<p>By the way, this is a great project!  This beats all of the other pre-PHP-5.3 ORM packages that I&#8217;ve tried, hands down.  It&#8217;s still going to be a while before everyone&#8217;s supporting PHP 5.3, so IMHO it&#8217;s worth it to make this a &#8220;real&#8221; project with releases, user forums, and a public git repo.  I&#8217;d be happy to help if the author is interested?  </p>
<p>Cheers,<br />
-Ben</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sebastian</title>
		<link>http://lukebaker.org/projects/activerecord-in-php/documentation/comment-page-1/#comment-22797</link>
		<dc:creator>Sebastian</dc:creator>
		<pubDate>Mon, 11 Jan 2010 14:09:40 +0000</pubDate>
		<guid isPermaLink="false">http://lukebaker.org/projects/activerecord-in-php/documentation/#comment-22797</guid>
		<description>An additional function: count
Same parameters as find, but only returns the number of rows.

Code:

In ActiveRecord.php add:

static function count($class, $options=null) {
    $class = str_replace(&#039;Base&#039;, &#039;&#039;, $class);
    $query = self::generate_count_query($class, $options);
    $rows = self::query($query);
    
    return $rows[0][&#039;COUNT(*)&#039;];
    
  }
	
  function generate_count_query($class_name, $options){
  	$item = new $class_name;
  	
  	/* regex for limit, order, group */
  	$regex = &#039;/^[A-Za-z0-9\-_ ,\(\)]+$/&#039;;
  	if (!isset($options[&#039;limit&#039;]) &#124;&#124; !preg_match($regex, $options[&#039;limit&#039;]))
  	$options[&#039;limit&#039;] = &#039;&#039;;
  	if (!isset($options[&#039;order&#039;]) &#124;&#124; !preg_match($regex, $options[&#039;order&#039;]))
  	$options[&#039;order&#039;] = &#039;&#039;;
  	if (!isset($options[&#039;group&#039;]) &#124;&#124; !preg_match($regex, $options[&#039;group&#039;]))
  	$options[&#039;group&#039;] = &#039;&#039;;
  	if (!isset($options[&#039;offset&#039;]) &#124;&#124; !is_numeric($options[&#039;offset&#039;]))
  	$options[&#039;offset&#039;] = &#039;&#039;;

  	$select = &#039;COUNT(*)&#039;;
  	
  	if (isset($options[&#039;conditions&#039;]))
  		$where = (isset($where) &amp;&amp; $where) ? $where . &quot; AND (&quot; . $options[&#039;conditions&#039;] .&quot;)&quot; : $options[&#039;conditions&#039;];
  	if ($options[&#039;offset&#039;])
  		$offset = $options[&#039;offset&#039;];
  	if ($options[&#039;limit&#039;] &amp;&amp; !isset($limit))
  		$limit = $options[&#039;limit&#039;];

  	$joins = array();
  	$tables_to_columns = array();
  	

  	$query  = &quot;SELECT $select FROM {$item-&gt;table_name}&quot;;
  	$query .= (isset($where)) ? &quot; WHERE $where&quot; : &quot;&quot;;
  	$query .= ($options[&#039;group&#039;]) ? &quot; GROUP BY {$options[&#039;group&#039;]}&quot; : &quot;&quot;;
  	$query .= ($options[&#039;order&#039;]) ? &quot; ORDER BY {$options[&#039;order&#039;]}&quot; : &quot;&quot;;
  	$query .= (isset($limit) &amp;&amp; $limit) ? &quot; LIMIT $limit&quot; : &quot;&quot;;
  	$query .= (isset($offset) &amp;&amp; $offset) ? &quot; OFFSET $offset&quot; : &quot;&quot;;
	
  	return $query;
  }

In ModelBase.tpl add inside the Class definition:

static function count($options=null){
    return parent::count(__CLASS__,$options);
  }</description>
		<content:encoded><![CDATA[<p>An additional function: count<br />
Same parameters as find, but only returns the number of rows.</p>
<p>Code:</p>
<p>In ActiveRecord.php add:</p>
<p>static function count($class, $options=null) {<br />
    $class = str_replace(&#8216;Base&#8217;, &#8221;, $class);<br />
    $query = self::generate_count_query($class, $options);<br />
    $rows = self::query($query);</p>
<p>    return $rows[0]['COUNT(*)'];</p>
<p>  }</p>
<p>  function generate_count_query($class_name, $options){<br />
  	$item = new $class_name;</p>
<p>  	/* regex for limit, order, group */<br />
  	$regex = &#8216;/^[A-Za-z0-9\-_ ,\(\)]+$/&#8217;;<br />
  	if (!isset($options['limit']) || !preg_match($regex, $options['limit']))<br />
  	$options['limit'] = &#8221;;<br />
  	if (!isset($options['order']) || !preg_match($regex, $options['order']))<br />
  	$options['order'] = &#8221;;<br />
  	if (!isset($options['group']) || !preg_match($regex, $options['group']))<br />
  	$options['group'] = &#8221;;<br />
  	if (!isset($options['offset']) || !is_numeric($options['offset']))<br />
  	$options['offset'] = &#8221;;</p>
<p>  	$select = &#8216;COUNT(*)&#8217;;</p>
<p>  	if (isset($options['conditions']))<br />
  		$where = (isset($where) &amp;&amp; $where) ? $where . &#8221; AND (&#8221; . $options['conditions'] .&#8221;)&#8221; : $options['conditions'];<br />
  	if ($options['offset'])<br />
  		$offset = $options['offset'];<br />
  	if ($options['limit'] &amp;&amp; !isset($limit))<br />
  		$limit = $options['limit'];</p>
<p>  	$joins = array();<br />
  	$tables_to_columns = array();</p>
<p>  	$query  = &#8220;SELECT $select FROM {$item-&gt;table_name}&#8221;;<br />
  	$query .= (isset($where)) ? &#8221; WHERE $where&#8221; : &#8220;&#8221;;<br />
  	$query .= ($options['group']) ? &#8221; GROUP BY {$options['group']}&#8221; : &#8220;&#8221;;<br />
  	$query .= ($options['order']) ? &#8221; ORDER BY {$options['order']}&#8221; : &#8220;&#8221;;<br />
  	$query .= (isset($limit) &amp;&amp; $limit) ? &#8221; LIMIT $limit&#8221; : &#8220;&#8221;;<br />
  	$query .= (isset($offset) &amp;&amp; $offset) ? &#8221; OFFSET $offset&#8221; : &#8220;&#8221;;</p>
<p>  	return $query;<br />
  }</p>
<p>In ModelBase.tpl add inside the Class definition:</p>
<p>static function count($options=null){<br />
    return parent::count(__CLASS__,$options);<br />
  }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthieu</title>
		<link>http://lukebaker.org/projects/activerecord-in-php/documentation/comment-page-1/#comment-22775</link>
		<dc:creator>Matthieu</dc:creator>
		<pubDate>Wed, 06 Jan 2010 18:32:00 +0000</pubDate>
		<guid isPermaLink="false">http://lukebaker.org/projects/activerecord-in-php/documentation/#comment-22775</guid>
		<description>Another bug i found:

in file AcitveRecord.php, on around 317, change this:

if ($table_name == ActiveRecordInflector::pluralize($assoc_name))

to this:

if ($table_name == ActiveRecordInflector::tableize($assoc_name))</description>
		<content:encoded><![CDATA[<p>Another bug i found:</p>
<p>in file AcitveRecord.php, on around 317, change this:</p>
<p>if ($table_name == ActiveRecordInflector::pluralize($assoc_name))</p>
<p>to this:</p>
<p>if ($table_name == ActiveRecordInflector::tableize($assoc_name))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthieu</title>
		<link>http://lukebaker.org/projects/activerecord-in-php/documentation/comment-page-1/#comment-22773</link>
		<dc:creator>Matthieu</dc:creator>
		<pubDate>Wed, 06 Jan 2010 16:36:45 +0000</pubDate>
		<guid isPermaLink="false">http://lukebaker.org/projects/activerecord-in-php/documentation/#comment-22773</guid>
		<description>Little bug correction:

in HasMany.php, around line 58, change this:

        else {
          // TODO: $this-&gt;options[&#039;through&#039;] is not necessarily the table name
          $collection = call_user_func_array(array($this-&gt;dest_class, &#039;find&#039;),
            array(&#039;all&#039;,
              array(&#039;include&#039; =&gt; $this-&gt;options[&#039;through&#039;],
                &#039;conditions&#039; =&gt; &quot;{$this-&gt;options[&#039;through&#039;]}.{$this-&gt;foreign_key} = &quot;.$source-&gt;{$source-&gt;get_primary_key()})));
        }

to this :

        else {
          $through_table = ActiveRecordInflector::tableize($this-&gt;options[&#039;through&#039;]);
          $collection = call_user_func_array(array($this-&gt;dest_class, &#039;find&#039;),
            array(&#039;all&#039;,
              array(&#039;include&#039; =&gt; $this-&gt;options[&#039;through&#039;],
                &#039;conditions&#039; =&gt; &quot;{$through_table}.{$this-&gt;foreign_key} = &quot;.$source-&gt;{$source-&gt;get_primary_key()})));
        }


And around line 144, modify this:

      $join = &quot;LEFT OUTER JOIN {$this-&gt;options[&#039;through&#039;]} ON &quot;
            . &quot;{$this-&gt;options[&#039;through&#039;]}.{$this-&gt;foreign_key} = $source_table.&quot;.$source_inst-&gt;get_primary_key() .&quot; &quot;
            . &quot;LEFT OUTER JOIN $dest_table ON &quot;
            . &quot;$dest_table.&quot;.$dest_inst-&gt;get_primary_key() .&quot; = {$this-&gt;options[&#039;through&#039;]}.&quot; . ActiveRecordInflector::foreign_key($this-&gt;dest_class);

to this:

      $through_table = ActiveRecordInflector::tableize($this-&gt;options[&#039;through&#039;]);
      $join = &quot;LEFT OUTER JOIN {$through_table} ON &quot;
            . &quot;{$through_table}.{$this-&gt;foreign_key} = $source_table.&quot;.$source_inst-&gt;get_primary_key() .&quot; &quot;
            . &quot;LEFT OUTER JOIN $dest_table ON &quot;
            . &quot;$dest_table.&quot;.$dest_inst-&gt;get_primary_key() .&quot; = {$through_table}.&quot; . ActiveRecordInflector::foreign_key($this-&gt;dest_class);</description>
		<content:encoded><![CDATA[<p>Little bug correction:</p>
<p>in HasMany.php, around line 58, change this:</p>
<p>        else {<br />
          // TODO: $this-&gt;options['through'] is not necessarily the table name<br />
          $collection = call_user_func_array(array($this-&gt;dest_class, &#8216;find&#8217;),<br />
            array(&#8216;all&#8217;,<br />
              array(&#8216;include&#8217; =&gt; $this-&gt;options['through'],<br />
                &#8216;conditions&#8217; =&gt; &#8220;{$this-&gt;options['through']}.{$this-&gt;foreign_key} = &#8220;.$source-&gt;{$source-&gt;get_primary_key()})));<br />
        }</p>
<p>to this :</p>
<p>        else {<br />
          $through_table = ActiveRecordInflector::tableize($this-&gt;options['through']);<br />
          $collection = call_user_func_array(array($this-&gt;dest_class, &#8216;find&#8217;),<br />
            array(&#8216;all&#8217;,<br />
              array(&#8216;include&#8217; =&gt; $this-&gt;options['through'],<br />
                &#8216;conditions&#8217; =&gt; &#8220;{$through_table}.{$this-&gt;foreign_key} = &#8220;.$source-&gt;{$source-&gt;get_primary_key()})));<br />
        }</p>
<p>And around line 144, modify this:</p>
<p>      $join = &#8220;LEFT OUTER JOIN {$this-&gt;options['through']} ON &#8221;<br />
            . &#8220;{$this-&gt;options['through']}.{$this-&gt;foreign_key} = $source_table.&#8221;.$source_inst-&gt;get_primary_key() .&#8221; &#8221;<br />
            . &#8220;LEFT OUTER JOIN $dest_table ON &#8221;<br />
            . &#8220;$dest_table.&#8221;.$dest_inst-&gt;get_primary_key() .&#8221; = {$this-&gt;options['through']}.&#8221; . ActiveRecordInflector::foreign_key($this-&gt;dest_class);</p>
<p>to this:</p>
<p>      $through_table = ActiveRecordInflector::tableize($this-&gt;options['through']);<br />
      $join = &#8220;LEFT OUTER JOIN {$through_table} ON &#8221;<br />
            . &#8220;{$through_table}.{$this-&gt;foreign_key} = $source_table.&#8221;.$source_inst-&gt;get_primary_key() .&#8221; &#8221;<br />
            . &#8220;LEFT OUTER JOIN $dest_table ON &#8221;<br />
            . &#8220;$dest_table.&#8221;.$dest_inst-&gt;get_primary_key() .&#8221; = {$through_table}.&#8221; . ActiveRecordInflector::foreign_key($this-&gt;dest_class);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fyodor</title>
		<link>http://lukebaker.org/projects/activerecord-in-php/documentation/comment-page-1/#comment-21589</link>
		<dc:creator>Fyodor</dc:creator>
		<pubDate>Mon, 29 Jun 2009 11:48:29 +0000</pubDate>
		<guid isPermaLink="false">http://lukebaker.org/projects/activerecord-in-php/documentation/#comment-21589</guid>
		<description>Sorry for code formatting and my bad English. ;)</description>
		<content:encoded><![CDATA[<p>Sorry for code formatting and my bad English. ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fyodor</title>
		<link>http://lukebaker.org/projects/activerecord-in-php/documentation/comment-page-1/#comment-21588</link>
		<dc:creator>Fyodor</dc:creator>
		<pubDate>Mon, 29 Jun 2009 11:47:16 +0000</pubDate>
		<guid isPermaLink="false">http://lukebaker.org/projects/activerecord-in-php/documentation/#comment-21588</guid>
		<description>For those seeking to :foreign_key and :class_name options for associations, you may modify following classes:

1. Association in Association.php:

  ...
  function __construct($source, $dest, $options=null) {
    $this-&gt;source_class = get_class($source);

    if (!empty($options[&#039;foreign_key&#039;])) {
      $this-&gt;foreign_key = $options[&#039;foreign_key&#039;];
    }

    if (!empty($options[&#039;class_name&#039;])) {
      $this-&gt;dest_class = $options[&#039;class_name&#039;];
    } else {
      $this-&gt;dest_class = ActiveRecordInflector::classify($dest);
    }

    $this-&gt;options = $options;
  }
  ...

2. BelongsTo in BelongsTo.php:
  ...
  function __construct(&amp;$source, $dest, $options=null) {
    parent::__construct($source, $dest, $options);
    if (!isset($this-&gt;foreign_key)) {
      $this-&gt;foreign_key = ActiveRecordInflector::foreign_key($this-&gt;dest_class);
    }
  }
  ...

3. HasMany in HasMany.php, HasOne in HasOne.php:
  ...
  function __construct(&amp;$source, $dest, $options=null) {
    parent::__construct($source, $dest, $options);
    if (!isset($this-&gt;foreign_key)) {
      $this-&gt;foreign_key = ActiveRecordInflector::foreign_key($this-&gt;source_class);
    }
  }
  ...

Example:

class Product extends ProductBase {
  protected $belongs_to = array(array(&#039;color1&#039; =&gt; array(&#039;class_name&#039; =&gt; &#039;Color&#039;, &#039;foreign_key&#039; =&gt; &#039;color1&#039;)), array(&#039;color2&#039; =&gt; array(&#039;class_name&#039; =&gt; &#039;Color&#039;, &#039;foreign_key&#039; =&gt; &#039;color2&#039;)), array(&#039;color3&#039; =&gt; array(&#039;class_name&#039; =&gt; &#039;Color&#039;, &#039;foreign_key&#039; =&gt; &#039;color3&#039;)));
}

Solution is not perfect, just quick patch.</description>
		<content:encoded><![CDATA[<p>For those seeking to :foreign_key and :class_name options for associations, you may modify following classes:</p>
<p>1. Association in Association.php:</p>
<p>  &#8230;<br />
  function __construct($source, $dest, $options=null) {<br />
    $this-&gt;source_class = get_class($source);</p>
<p>    if (!empty($options['foreign_key'])) {<br />
      $this-&gt;foreign_key = $options['foreign_key'];<br />
    }</p>
<p>    if (!empty($options['class_name'])) {<br />
      $this-&gt;dest_class = $options['class_name'];<br />
    } else {<br />
      $this-&gt;dest_class = ActiveRecordInflector::classify($dest);<br />
    }</p>
<p>    $this-&gt;options = $options;<br />
  }<br />
  &#8230;</p>
<p>2. BelongsTo in BelongsTo.php:<br />
  &#8230;<br />
  function __construct(&amp;$source, $dest, $options=null) {<br />
    parent::__construct($source, $dest, $options);<br />
    if (!isset($this-&gt;foreign_key)) {<br />
      $this-&gt;foreign_key = ActiveRecordInflector::foreign_key($this-&gt;dest_class);<br />
    }<br />
  }<br />
  &#8230;</p>
<p>3. HasMany in HasMany.php, HasOne in HasOne.php:<br />
  &#8230;<br />
  function __construct(&amp;$source, $dest, $options=null) {<br />
    parent::__construct($source, $dest, $options);<br />
    if (!isset($this-&gt;foreign_key)) {<br />
      $this-&gt;foreign_key = ActiveRecordInflector::foreign_key($this-&gt;source_class);<br />
    }<br />
  }<br />
  &#8230;</p>
<p>Example:</p>
<p>class Product extends ProductBase {<br />
  protected $belongs_to = array(array(&#8216;color1&#8242; =&gt; array(&#8216;class_name&#8217; =&gt; &#8216;Color&#8217;, &#8216;foreign_key&#8217; =&gt; &#8216;color1&#8242;)), array(&#8216;color2&#8242; =&gt; array(&#8216;class_name&#8217; =&gt; &#8216;Color&#8217;, &#8216;foreign_key&#8217; =&gt; &#8216;color2&#8242;)), array(&#8216;color3&#8242; =&gt; array(&#8216;class_name&#8217; =&gt; &#8216;Color&#8217;, &#8216;foreign_key&#8217; =&gt; &#8216;color3&#8242;)));<br />
}</p>
<p>Solution is not perfect, just quick patch.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luke</title>
		<link>http://lukebaker.org/projects/activerecord-in-php/documentation/comment-page-1/#comment-21336</link>
		<dc:creator>Luke</dc:creator>
		<pubDate>Sat, 09 May 2009 02:50:53 +0000</pubDate>
		<guid isPermaLink="false">http://lukebaker.org/projects/activerecord-in-php/documentation/#comment-21336</guid>
		<description>Brian,

I agree it is not always self-evident what is happening.  When I created this I based it off of the ActiveRecord that was shipping with Ruby on Rails at the time.  Unfortunately, this PHP version hasn&#039;t kept pace with the Ruby on Rails version, so I can&#039;t simply point you to that documentation.

I&#039;ll answer your last question first, as that might be helpful as I answer the others.  The arrays that are returned by calls like $post-&gt;comments are your typical PHP arrays with nothing fancy.  So, count($post-&gt;comments) should work.  However, you won&#039;t be able to do anything fancy like unset or pop an element of that array using the core PHP functions and have that affect your database.

If you want to add or remove elements you can use the $post-&gt;comments_push() and $post-&gt;comments_pop() functions.  You can also get an array of the comment ids that belong to a post with the following call $post-&gt;comment_ids.  Similarly you can add or remove comments by doing $post-&gt;comment_ids = array(1, 3, 4).  That makes sure that the only comments that belong to that post have ids of 1, 3, or 4.  $post-&gt;comment_ids = array();  would then remove all associated with that post.  If that&#039;s a &quot;has_many through&quot; relationship, then the comment will remain, but be no longer associated with the post referenced.

When you do something like:

$post-&gt;comments[$i]-&gt;comment_text = $RevisedText;

it does not save the revised text.  You must issue a save() for that.  You can either do $post-&gt;save() or $post-&gt;comments[$i]-&gt;save().  The reasoning behind this, is you that might want to update multiple attributes on the comment, but only issue one UPDATE or INSERT into the database.

When you do $post-&gt;comments_push($comment), the code expects that you want that saved to the database immediately.

If you want to dig in more, I&#039;d encourage you to take a look at the development branch which has a myriad of tests.  Examining those will give you some more info as to what should be happening.  In fact, I went back to those tests to answer some of these questions, since it has been awhile. :-)

http://lukebaker.org/svn/repos/activerecord/branches/devel/

Luke</description>
		<content:encoded><![CDATA[<p>Brian,</p>
<p>I agree it is not always self-evident what is happening.  When I created this I based it off of the ActiveRecord that was shipping with Ruby on Rails at the time.  Unfortunately, this PHP version hasn&#8217;t kept pace with the Ruby on Rails version, so I can&#8217;t simply point you to that documentation.</p>
<p>I&#8217;ll answer your last question first, as that might be helpful as I answer the others.  The arrays that are returned by calls like $post->comments are your typical PHP arrays with nothing fancy.  So, count($post->comments) should work.  However, you won&#8217;t be able to do anything fancy like unset or pop an element of that array using the core PHP functions and have that affect your database.</p>
<p>If you want to add or remove elements you can use the $post->comments_push() and $post->comments_pop() functions.  You can also get an array of the comment ids that belong to a post with the following call $post->comment_ids.  Similarly you can add or remove comments by doing $post->comment_ids = array(1, 3, 4).  That makes sure that the only comments that belong to that post have ids of 1, 3, or 4.  $post->comment_ids = array();  would then remove all associated with that post.  If that&#8217;s a &#8220;has_many through&#8221; relationship, then the comment will remain, but be no longer associated with the post referenced.</p>
<p>When you do something like:</p>
<p>$post->comments[$i]->comment_text = $RevisedText;</p>
<p>it does not save the revised text.  You must issue a save() for that.  You can either do $post->save() or $post->comments[$i]->save().  The reasoning behind this, is you that might want to update multiple attributes on the comment, but only issue one UPDATE or INSERT into the database.</p>
<p>When you do $post->comments_push($comment), the code expects that you want that saved to the database immediately.</p>
<p>If you want to dig in more, I&#8217;d encourage you to take a look at the development branch which has a myriad of tests.  Examining those will give you some more info as to what should be happening.  In fact, I went back to those tests to answer some of these questions, since it has been awhile. :-)</p>
<p><a href="http://lukebaker.org/svn/repos/activerecord/branches/devel/" rel="nofollow">http://lukebaker.org/svn/repos/activerecord/branches/devel/</a></p>
<p>Luke</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Yoder</title>
		<link>http://lukebaker.org/projects/activerecord-in-php/documentation/comment-page-1/#comment-21303</link>
		<dc:creator>Brian Yoder</dc:creator>
		<pubDate>Thu, 07 May 2009 08:14:02 +0000</pubDate>
		<guid isPermaLink="false">http://lukebaker.org/projects/activerecord-in-php/documentation/#comment-21303</guid>
		<description>Maybe I am misunderstanding something here.  If I have a many to many relationship between a couple of tables with a link table between them containing IDs of both tables in it, can I just read into the big object and say something like this?

echo $post-&gt;comments[$i]-&gt;comment_text.&#039; is my is the ith comment text!&#039;;

And if I want to change a value can I just say something like this?

$post-&gt;comments[$i]-&gt;comment_text = $RevisedText;

Does it really save the value instantly when I do that?  Or only when I do a $post-&gt;save(); ?  Or is that only what happens when I do this?

$post-&gt;comment_push($mycomment);

And what if I want to disassociate one of the comments from the post (but leave it out there because this is a many to many relationship and some other post may share the same comment?  Could I just clean out the array like this?

unset($post-&gt;comments);

Or could I pop all of the elements off like this?

foreach ($post-&gt;comments as $MyComment)
{
   array_pop($MyComment);
}

Or perhaps I could remove the fourth comment from the array via:

$post-&gt;comments[3] = array();

I have played around with many of these and they don&#039;t seem to work the way I am thinking they should.  This makes me wonder just what kind of thing these arrays are and how you access them. If I can&#039;t just put values in there, how can I really change them? You show a few specific calls I can make (to push and pop items from the list for example) but is that really all you can do to them?  It would seem that there ought to be more operators than just those, are there?  Or do I just have to compose all higher level access to them in terms of looking, pushing, and popping?

Sorry for asking so many questions, but the documentation page and the experiments I have been trying leave me with lot of questions.

Thanks for all your help!

Brian</description>
		<content:encoded><![CDATA[<p>Maybe I am misunderstanding something here.  If I have a many to many relationship between a couple of tables with a link table between them containing IDs of both tables in it, can I just read into the big object and say something like this?</p>
<p>echo $post-&gt;comments[$i]-&gt;comment_text.&#8217; is my is the ith comment text!&#8217;;</p>
<p>And if I want to change a value can I just say something like this?</p>
<p>$post-&gt;comments[$i]-&gt;comment_text = $RevisedText;</p>
<p>Does it really save the value instantly when I do that?  Or only when I do a $post-&gt;save(); ?  Or is that only what happens when I do this?</p>
<p>$post-&gt;comment_push($mycomment);</p>
<p>And what if I want to disassociate one of the comments from the post (but leave it out there because this is a many to many relationship and some other post may share the same comment?  Could I just clean out the array like this?</p>
<p>unset($post-&gt;comments);</p>
<p>Or could I pop all of the elements off like this?</p>
<p>foreach ($post-&gt;comments as $MyComment)<br />
{<br />
   array_pop($MyComment);<br />
}</p>
<p>Or perhaps I could remove the fourth comment from the array via:</p>
<p>$post-&gt;comments[3] = array();</p>
<p>I have played around with many of these and they don&#8217;t seem to work the way I am thinking they should.  This makes me wonder just what kind of thing these arrays are and how you access them. If I can&#8217;t just put values in there, how can I really change them? You show a few specific calls I can make (to push and pop items from the list for example) but is that really all you can do to them?  It would seem that there ought to be more operators than just those, are there?  Or do I just have to compose all higher level access to them in terms of looking, pushing, and popping?</p>
<p>Sorry for asking so many questions, but the documentation page and the experiments I have been trying leave me with lot of questions.</p>
<p>Thanks for all your help!</p>
<p>Brian</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Yoder</title>
		<link>http://lukebaker.org/projects/activerecord-in-php/documentation/comment-page-1/#comment-21245</link>
		<dc:creator>Brian Yoder</dc:creator>
		<pubDate>Mon, 04 May 2009 08:21:20 +0000</pubDate>
		<guid isPermaLink="false">http://lukebaker.org/projects/activerecord-in-php/documentation/#comment-21245</guid>
		<description>So what is the idea behind generate.php generating no UI at all?  Why not just have it say something like &quot;Generation complete&quot; or &quot;52 models created.&quot; or something?</description>
		<content:encoded><![CDATA[<p>So what is the idea behind generate.php generating no UI at all?  Why not just have it say something like &#8220;Generation complete&#8221; or &#8220;52 models created.&#8221; or something?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.233 seconds -->
