1
0
mirror of synced 2024-12-15 07:36:03 +03:00
doctrine2/manual/docs/en/working-with-objects/component-overview/table.txt

92 lines
2.0 KiB
Plaintext
Raw Normal View History

++++ Introduction
++++ Getting table information
<code type="php">
$table = $conn->getTable('User');
// getting column names
$names = $table->getColumnNames();
// getting column information
$columns = $table->getColumns();
</code>
++++ Finder methods
{{Doctrine_Table}} provides basic finder methods. These finder methods are very fast and should be used if you only need to fetch data from one database table. If you need queries that use several components (database tables) use {{Doctrine_Connection::query()}}.
<code type="php">
$table = $conn->getTable("User");
// find by primary key
$user = $table->find(2);
if($user !== false)
print $user->name;
// get all users
foreach($table->findAll() as $user) {
print $user->name;
}
// finding by dql
foreach($table->findByDql("name LIKE '%John%'") as $user) {
print $user->created;
}
</code>
++++ Custom table classes
Adding custom table classes is very easy. Only thing you need to do is name the classes as {{[componentName]Table}} and make them inherit {{Doctrine_Table}}.
<code type="php">
// valid table object
class UserTable extends Doctrine_Table {
}
// not valid [doesn't extend Doctrine_Table]
class GroupTable { }
</code>
++++ Custom finders
You can add custom finder methods to your custom table object. These finder methods may use fast {{Doctrine_Table}} finder methods or DQL API ({{Doctrine_Connection::query()}}).
<code type="php">
class UserTable extends Doctrine_Table {
/**
* you can add your own finder methods here
*/
public function findByName($name) {
return $this->getConnection()->query("FROM User WHERE name LIKE '%$name%'");
}
}
class User extends Doctrine_Record { }
$conn = Doctrine_Manager::getInstance()
->openConnection(new PDO("dsn","username","password"));
// doctrine will now check if a class called UserTable exists
// and if it inherits Doctrine_Table
$table = $conn->getTable("User");
print get_class($table); // UserTable
$users = $table->findByName("Jack");
</code>
++++ Getting relation objects