/** * Returns the number of records matching the given WHERE clause. * * @param string $whereClause * WHERE clause for the number of records to retrieve, must be quoted * and SQL safe, may be empty * * @return int the number of records matching the given WHERE clause */ public function countByWhereClause($whereClause = '') { $completeWhereClause = $whereClause === '' ? '' : $whereClause . ' AND '; return Tx_Oelib_Db::count($this->getTableName(), $completeWhereClause . $this->getUniversalWhereClause()); }
/** * @test */ public function removeRelationOnRealRecordNotRemovesRelation() { $uidLocal = $this->subject->createRecord('tx_oelib_test'); $uidForeign = $this->subject->createRecord('tx_oelib_test'); // Create a new record that looks like a real record, i.e. the // is_dummy_record flag is set to 0. Tx_Oelib_Db::insert('tx_oelib_test_article_mm', array('uid_local' => $uidLocal, 'uid_foreign' => $uidForeign, 'is_dummy_record' => 0)); // Runs our delete method which should NOT affect the record created // above. $this->subject->removeRelation('tx_oelib_test_article_mm', $uidLocal, $uidForeign); // Caches the value that will be tested for later. We need to use the // following order to make sure the test record gets deleted even if // this test fails: // 1. reads the value to test // 2. deletes the test record // 3. tests the previously read value (and possibly fails) $numberOfCreatedRelations = Tx_Oelib_Db::count('tx_oelib_test_article_mm', 'uid_local = ' . $uidLocal . ' AND uid_foreign = ' . $uidForeign); // Deletes the record as it will not be caught by the clean up function. Tx_Oelib_Db::delete('tx_oelib_test_article_mm', 'uid_local = ' . $uidLocal . ' AND uid_foreign = ' . $uidForeign . ' AND is_dummy_record = 0'); // Checks whether the relation had been created further up. self::assertSame(1, $numberOfCreatedRelations); }
/** * @test * * @expectedException BadMethodCallException */ public function countDoesNotAllowJoinWithOnlyOneTableOnTheRight() { Tx_Oelib_Db::count('JOIN tx_oelib_test'); }
/** * Counts the dummy records in the table given by the first parameter $table * that match a given WHERE clause. * * @param string $table the name of the table to query, must not be empty * @param string $whereClause the WHERE part of the query, may be empty (all records will be counted in that case) * * @return int the number of records that have been found, will be >= 0 * * @throws InvalidArgumentException */ public function countRecords($table, $whereClause = '') { if (!$this->isTableNameAllowed($table)) { throw new InvalidArgumentException('The given table name is invalid. This means it is either empty or not in the list of allowed tables.', 1331490862); } $whereForDummyColumn = $this->getDummyColumnName($table) . ' = 1'; $compoundWhereClause = $whereClause !== '' ? '(' . $whereClause . ') AND ' . $whereForDummyColumn : $whereForDummyColumn; return Tx_Oelib_Db::count($table, $compoundWhereClause); }