Example #1
0
 public function testSetData()
 {
     $col = new Collection();
     $col->setData(array());
     $this->assertEquals(array(), $col->getArrayCopy(), 'setData() can set data to an empty array');
     $data = array('bar1', 'bar2', 'bar3');
     $col = new Collection();
     $col->setData($data);
     $this->assertEquals($data, $col->getArrayCopy(), 'setData() sets the collection data');
 }
 /**
  * Synonym for toArray(), to provide a similar interface to PropelObjectCollection
  *
  * @param string  $keyColumn
  * @param boolean $usePrefix
  *
  * @return array
  */
 public function getArrayCopy($keyColumn = null, $usePrefix = false)
 {
     if (null === $keyColumn && false === $usePrefix) {
         return parent::getArrayCopy();
     }
     return $this->toArray($keyColumn, $usePrefix);
 }
Example #3
0
 /**
  * Get an array representation of the collection
  *
  * @param string  $keyColumn If null, the returned array uses an incremental index.
  *                           Otherwise, the array is indexed using the specified column
  * @param boolean $usePrefix If true, the returned array prefixes keys
  *                           with the model class name ('Article_0', 'Article_1', etc).
  *
  * <code>
  *   $bookCollection->getArrayCopy();
  *   array(
  *    0 => $book0,
  *    1 => $book1,
  *   )
  *   $bookCollection->getArrayCopy('Id');
  *   array(
  *    123 => $book0,
  *    456 => $book1,
  *   )
  *   $bookCollection->getArrayCopy(null, true);
  *   array(
  *    'Book_0' => $book0,
  *    'Book_1' => $book1,
  *   )
  * </code>
  *
  * @return array
  */
 public function getArrayCopy($keyColumn = null, $usePrefix = false)
 {
     if (null === $keyColumn && false === $usePrefix) {
         return parent::getArrayCopy();
     }
     $ret = [];
     $keyGetterMethod = 'get' . $keyColumn;
     foreach ($this as $key => $obj) {
         $key = null === $keyColumn ? $key : $obj->{$keyGetterMethod}();
         $key = $usePrefix ? $this->getModel() . '_' . $key : $key;
         $ret[$key] = $obj;
     }
     return $ret;
 }