diff options
Diffstat (limited to 'demos/quickstart')
-rw-r--r-- | demos/quickstart/protected/controls/pl/Layout.tpl | 61 | ||||
-rw-r--r-- | demos/quickstart/protected/controls/pl/RequiresVersion.tpl | 1 | ||||
-rw-r--r-- | demos/quickstart/protected/controls/pl/SearchBox.tpl | 3 | ||||
-rw-r--r-- | demos/quickstart/protected/controls/pl/SinceVersion.tpl | 1 | ||||
-rw-r--r-- | demos/quickstart/protected/pages/Database/pl/ActiveRecord.page | 1163 | ||||
-rw-r--r-- | demos/quickstart/protected/pages/Database/pl/ar_objects.png | bin | 0 -> 20638 bytes | |||
-rw-r--r-- | demos/quickstart/protected/pages/Database/pl/ar_relations.png | bin | 0 -> 9693 bytes | |||
-rw-r--r-- | demos/quickstart/protected/pages/Database/pl/diagram.png | bin | 0 -> 30320 bytes | |||
-rwxr-xr-x | demos/quickstart/protected/pages/Database/pl/object_states.png | bin | 0 -> 9596 bytes | |||
-rwxr-xr-x | demos/quickstart/protected/pages/Database/pl/sqlmap_active_record.png | bin | 0 -> 17351 bytes | |||
-rw-r--r-- | demos/quickstart/protected/pages/pl/Construction.page | 5 | ||||
-rw-r--r-- | demos/quickstart/protected/pages/pl/Search.page | 29 | ||||
-rw-r--r-- | demos/quickstart/protected/pages/pl/ViewSource.page | 31 |
13 files changed, 1294 insertions, 0 deletions
diff --git a/demos/quickstart/protected/controls/pl/Layout.tpl b/demos/quickstart/protected/controls/pl/Layout.tpl new file mode 100644 index 00000000..7fd86713 --- /dev/null +++ b/demos/quickstart/protected/controls/pl/Layout.tpl @@ -0,0 +1,61 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
+
+<com:THead Title="PRADO QuickStart Tutorial">
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+<meta http-equiv="content-language" content="en"/>
+</com:THead>
+
+<body>
+<com:TForm>
+<div id="header">
+<div class="title">Prado QuickStart Tutorial</div>
+<div class="image"></div>
+</div>
+
+<com:TPanel ID="MainMenu" CssClass="mainmenu">
+<div style="float:left; color:black; margin-top:-5px">
+ <com:SearchBox />
+</div>
+<a href="?">Główna</a> |
+<a href="http://www.pradosoft.com">PradoSoft.com</a> |
+<a href="../../docs/quickstart.pdf">wersja PDF</a> |
+<com:THyperLink ID="PrinterLink" Text="wersja do druku" />
+</com:TPanel>
+
+<table width="100%" border="0" cellspacing="0" cellpadding="0">
+<tr>
+<td valign="top" width="1">
+<com:TopicList ID="TopicPanel" />
+</td>
+<td valign="top">
+
+<com:TRepeater ID="languages" OnItemCreated="languageLinkCreated">
+ <prop:HeaderTemplate>
+ <div class="languages">Dostępne języki: <ul>
+ </prop:HeaderTemplate>
+ <prop:ItemTemplate>
+ <li><com:THyperLink ID="link" Text=<%# $this->DataItem %> /></li>
+ </prop:ItemTemplate>
+ <prop:FooterTemplate>
+ </ul></div>
+ </prop:FooterTemplate>
+</com:TRepeater>
+<div id="content">
+<p class="block-content" id="top-content" style="border-color: transparent; height:1px; margin: 0; padding: 0; background-color: transparent"></p>
+<com:TContentPlaceHolder ID="body" />
+</div>
+</td>
+</tr>
+</table>
+
+<div id="footer">
+Copyright © 2005-2007 <a href="http://www.pradosoft.com">PradoSoft</a>.
+<br/><br/>
+<%= Prado::poweredByPrado() %>
+<a href="http://validator.w3.org/check?uri=referer"><img border="0" src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a>
+</div>
+
+</com:TForm>
+</body>
+</html>
\ No newline at end of file diff --git a/demos/quickstart/protected/controls/pl/RequiresVersion.tpl b/demos/quickstart/protected/controls/pl/RequiresVersion.tpl new file mode 100644 index 00000000..01e70c43 --- /dev/null +++ b/demos/quickstart/protected/controls/pl/RequiresVersion.tpl @@ -0,0 +1 @@ +<p class="requires-version">Wymaga PRADO w wersji <strong><%= $this->Version %></strong> lub wyższej.</p>
\ No newline at end of file diff --git a/demos/quickstart/protected/controls/pl/SearchBox.tpl b/demos/quickstart/protected/controls/pl/SearchBox.tpl new file mode 100644 index 00000000..a00cbd72 --- /dev/null +++ b/demos/quickstart/protected/controls/pl/SearchBox.tpl @@ -0,0 +1,3 @@ +<com:TLabel ForControl="search" Text="Szukaj:" CssClass="searchLabel"/>
+<com:TTextBox ID="search" OnTextChanged="doSearch" CssClass="searchBox"/>
+<com:TButton ID="find" Text="Znajdź" OnClick="doSearch" CssClass="searchButton"/>
\ No newline at end of file diff --git a/demos/quickstart/protected/controls/pl/SinceVersion.tpl b/demos/quickstart/protected/controls/pl/SinceVersion.tpl new file mode 100644 index 00000000..46cc1211 --- /dev/null +++ b/demos/quickstart/protected/controls/pl/SinceVersion.tpl @@ -0,0 +1 @@ +<p class="since-version">Dostępny(a) w PRADO od wersji <strong><%= $this->Version %></strong> i dalej.</p>
\ No newline at end of file diff --git a/demos/quickstart/protected/pages/Database/pl/ActiveRecord.page b/demos/quickstart/protected/pages/Database/pl/ActiveRecord.page new file mode 100644 index 00000000..53bbdee5 --- /dev/null +++ b/demos/quickstart/protected/pages/Database/pl/ActiveRecord.page @@ -0,0 +1,1163 @@ +<com:TContent ID="body" > +<!-- $Id $ --> +<h1 id="138046">Rekord Aktywny (ang. Active Record)</h1> +<com:SinceVersion Version="3.1a" /> +<p id="690478" class="block-content">Rekordy Aktywne są obiektami, które opakowują wiersz w bazie danych lub widoku, + obudowują (ang. encapsulate) dostęp do bazy danych oraz dziedziny logiki dla tych danych. + Podstawą Rekordu Aktywnego są klasy biznesowe np. klasa + <tt>Produkty</tt>, które są bardzo podobne do struktury rekordu należącego do bazy danych. Każdy Rekord Aktywny jest odpowiedzialny + za zapisywanie i łądowanie danych do i z bazy danych.</p> +<div class="info"><b class="note">Info:</b> + Struktura danych Rekordu Sktywnego powinna zgadzać się ze strukturą tablicy w bazie danych. + Każda kolumna w bazie danych powinna posiadać odpowiadający element: zmienną lub właściwość w klasie Rekordu Aktywnego reprezentującego tablicę. +</div> + +<h2 id="138047">Kiedy używać?</h2> + <p id="690479" class="block-content">Rekord Aktywne jest dobrym wyborem dla dziedziny logiki, która nie jest zbyt złożona, tak jak tworzenie, odczyty, aktualizacje oraz usuwanie. + Pochocne (ang. derivations) oraz sprawdzenia bazujące na pojedyńczym rekordzie sprawdzają się dobrze w tej konstrukcji. + Rekord Aktywne ma podstawową zaletę, którą jest prostota. Łatwo jest stworzyć Rekord Aktywny, łatwo go również zrozuieć. + </p> + + <p id="690480" class="block-content">Jednakże, jeśli twoja logika biznesowa staje się coraz bardziej złożona, wkrótce będziesz chciał + używać bezpośrednich relacji, zbiorów, dziedziczenia twojego obiektu i tak dalej. Nie da się tego łatwo odwzorować za pomocą Rekordu Aktywnego, + a dodawanie ich po kawałku staje się bardzo kłopotliwe. Innym argumentem przeciw Rekordowi Aktywnemu jest fakt, że łączy model obiektowy z modelem baz danych. + To czyni trudniejszym refaktoring, gdy projekt idzie naprzód. + </p> + + <p id="690481" class="block-content">Alternatywą jest używanie wzorca Data Mapper (mapa danych), który odseparowuje role obiektu biznesowego od tego jak te obiekty są przechowywane. + Prado dostarcza + Prado provides a darmowy wybór pomiędzy rekordem aktywnym a <a href="?page=Database.SqlMap">SqlMap Data Mapper</a>. + SqlMap Data Mapper może być uzywany do wczytania obiektów Rekordu Aktywnego, i na odwrót, te Rekordy Aktywne mogą zostać użyte do aktualizacji bazy danych. + Związek pomiędzy Rekordem Aktywnym a <a href="?page=Database.SqlMap">SqlMap</a> przedstawiony jest na kolejnym diagramie. Więcej informacji związanych z SqlMap Data Mapper można znaleźć w + <a href="http://www.pradosoft.com/demos/sqlmap/">manualu SqlMap</a>. + <img src=<%~ sqlmap_active_record.png %> alt="Active Records and SqlMap DataMapper" id="fig:diagram.png" class="figure"/> + </p> + + <p id="690482" class="block-content"> + Klasa Rekordu aktywnego posiada funkcjonalność do przeprowadzenia następujących zadań: + </p> + <ul id="u1" class="block-content"> + <li>Tworzenie, zwracanie, aktualizowani i usuwanie recordów (CRUD)</li> + <li>metody wyszukujące obudowujące powszechnie używane zapytania SQL i zwracające obiekty Rekordu Aktywnego</li> + <li>Wydobywają relacje (powiazanych obcych obiektów) takie jak "posiada wiele" (ang. has many), "posiada jedno" (ang. has one), "należy do" (ang. belongs to) oraz wiele do wielu "many to many" poprzez tablice asocjacyjne.</li> + <li>Opóźnione ładowanie (ang. Lazy loading) relacji</li> + </ul> +<h2>Implikacje modelu</h2> +<p> +Implementacja wzorca Aktywnego Rekordu w PRADO nie zapewnia referencyjnej tożsamości (ang. referential identity). Każdy istniejący obiekt używający +Rekordu Aktywnego jest koopią danych z bazy danych. Na przykład jeśli zapytasz o konkretnego klienta i zostanie zwrócony obiekt <tt>Klient</tt>, +to następnym razem kiedy zapytasz o tego klienta otrzymasz spowrotem inną instancję obiektu <tt>Klient</tt>. To implikuje, że ścisłe porównianie (np. używając <tt>===</tt>) +zwróci fałsz, natomiast luźne porównianie (np. używając <tt>==</tt>) zwróci prawdę jeśli wartości obiektu są równe poprzez luźne porónanie. +<p> +<p> +Jest to implikacja modelu wynikająca z następującego pytania: +<i>"Czy myślisz o kliencie jako o obiekcie, którego któy jest tylko jeden, czy też myślisz o obiekcie na którym działasz jako o <b>kopii</b> bazy danych.</i> +Inne mapowania O/R implikują, że istnieje tylko jeden obiekt Klienta z KlientID 100 +Other O/R mappings will imply that there is only one Customer object with custID 100 i to dosłownie jest ten klient. +Jeśli pobierzesz klienta i zmienisz pole w nim, wtedy masz zmienionego tego klienta. +<i>"To kontroastuje z: zmieniłeś tą kopię klienta ale nie tamtą kopię. +Jeśli dwóch ludzi zaktualizuje kleinta z dwóch kopii obiektu, kto zaktualizuje pierwszy lub być może ostanie wygrywa."</i> [A. Hejlsberg 2003] +</p> + +<h2 id="142010">Wspierane bazy danych</h2> +<p id="p1" class="block-content"> +Implementacja Aktywnego Rekordu wykorzystuje kalsy <a href="?page=Database.DAO">Prado DAO</a> by uzyskać dostęp do danych. Aktualna implementacja Aktywnego Rekordu wspiera następujace bazy danych +</p> +<ul> + <li><a href="http://www.mysql.com">MySQL 4.1 i wyższe</a></li> + <li><a href="http://www.postgres.com">Postgres SQL 7.3 i wyższe</a></li> + <li><a href="http://www.sqlite.org">SQLite 2 i 3</a></li> + <li><a href="#">MS SQL 2000 i wyższe</a></li> + <li><a href="http://www.oracle.com">Oracle Database (alfa)</a></li> +</ul> +<p id="710009" class="block-content">Wsparcie dla pozostałych baz danych może zostać wprowadzone, keidy będzie dostatecne zapotrzebowanie</p> + +<h1 id="138048">Definiowanie Aktywnego Rekordu</h1> +<p id="690483" class="block-content">Rozważmy następującą tablicę "<tt>users</tt>", która zawiera dwie kolumny nazwane "<tt>username</tt>" oraz "<tt>email</tt>", + gdzie "<tt>username</tt>" jest kluczem głównym. +<com:TTextHighlighter Language="sql" CssClass="source block-content" id="code_690147"> +CREATE TABLE users +( + username VARCHAR( 20 ) NOT NULL , + email VARCHAR( 200 ) , + PRIMARY KEY ( username ) +); +</com:TTextHighlighter> +</p> +<p id="690484" class="block-content">Następnie zdefiniujemy naszą klasę Rekordu Aktywnego odpowiadającą tablicy "<tt>users</tt>". +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690148"> +class UserRecord extends TActiveRecord +{ + const TABLE='users'; //nazwa tablicy + + public $username; //kolumna nazwana "username" w tablicy "users" + public $email; + + /** + * @return TActiveRecord intancja finder rekordu aktywnego + */ + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} +</com:TTextHighlighter> +</p> +<p id="690485" class="block-content">Każda kolumna tablicy "<tt>users</tt>" musi posiadać odpowiadającą jej właściwość o tej samej nazwie co kolumna w tablicy w klasie <tt>UserRecord</tt>. + Oczywiście, możesz zdefiniować dodatkowe zmienne lub właściwości, które nie istnieją w strukturze tablicy. + Stała <tt>TABLE</tt> jest opcjonalna w klasie , kiedy nazwa klasy jest taka sama jak nazwa tablicy w bazie danych, w przeciwnym przypadku <tt>TABLE</tt> + musi określać nazwę tablicy, która odpowiada klasie Rekordu Aktywnego. +</p> + +<div class="tip"><b class="note">Tip:</b> +Możesz określić kwalifikowane (ang. qualified) nazwy tablic np dla MySQL, <tt>TABLE = "`bazadanych1`.`tablica1`"</tt>. +</div> + +<p class="block-content" id="ar_as_component"> + Odkąd <tt>TActiveRecord</tt> rozszerza <tt>TComponent</tt>, metody setter i getter mogą zostać zdefiniowane + by umożliwić kontrolę nad tym jak zmienne są ustawiane i zwracane. Na przykłąd dodanie właściwości <tt>$level</tt> + do klasy UserRecord: +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690149"> +class UserRecord extends TActiveRecord { + ... //istniejąca uprzednio część definicji + + private $_level; + public function setLevel($value) { + $this->_level=TPropertyValue::ensureInteger($value,0); + } + public function getLevel($value){ + return $this->_level; + } +} +</com:TTextHighlighter> +<p id="710010" class="block-content">Więcej szczegółów dotyczących TComponent można znaleźć <a href="?page=Fundamentals.Components">dokumentacji komponentów</a>. +Później użyjemy metod getter/setters by umożliwić opóźnione ładowanie (ang. lazy loading) obiektów relacji. +</p> + +<div class="info"><b class="note">Info:</b> +<tt>TActiveRecord</tt> może również działać z widokami poprzez przypisanie do stałej <tt>TABLE</tt> + odpowiedniej nazwy widoku. Jednakże obiektu zwracane przez widoki są tylko do odczytu, wywołanie metod <tt>save()</tt> lub <tt>delete()</tt> +spowoduje wywołanie wyjątku. +</div> + +<p id="690486" class="block-content"> + Metoda statyczna <tt>finder()</tt> zwraca instancję <tt>UserRecord</tt>, która może zostać użyta do załadowania rekordów z bazy. + Ładowanie rekordów za pomocą tej metody będzie omówione później. Statyczna metoda <tt>TActiveRecord::finder()</tt> + pobiera nazwę klasy Rekord Aktywnego jako parametr. +</p> + +<h2 id="138049">Ustanawianie połączenia z bazą danych</h2> +<p id="690487" class="block-content"> + Domyślne połączenie z bazą dla Rekordu Aktywnego może zostać ustawione następujaco. + Zobacz <a href="?page=Database.DAO">Ustanawianie połączenia z bazą</a> + by uzyskać ogólnie dalsze szczegóły odnośnie tworzenia połączenia z bazą danych. +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690150"> +//utwóz połączenie i przekaż je do menadżera Rekordu Aktywnego +$dsn = 'pgsql:host=localhost;dbname=test'; //Postgres SQL +$conn = new TDbConnection($dsn, 'dbuser','dbpass'); +TActiveRecordManager::getInstance()->setDbConnection($conn); +</com:TTextHighlighter> + +<p id="710011" class="block-content">Alternatywnie, możesz stworzyć klasę bazową i nadpisać metodę <tt>getDbConnection()</tt> +do zwracania połączenia z bazą. To jest prosty spodób, by umożliwić wielkokrotne połączenia do wielu baz danych. + Następujący kod demonstruje definiowanie połączenia z bazą danych w klasie bazowej (nie ma potrzeby by ustawiać połączenie DB gdziekolwiek indziej). +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content"> +class MyDb1Record extends TActiveRecord +{ + public function getDbConnection() + { + static $conn; + if($conn===null) + $conn = new TDbConnection('xxx','yyy','zzz'); + return $conn; + } +} +class MyDb2Record extends TActiveRecord +{ + public function getDbConnection() + { + static $conn; + if($conn===null) + $conn = new TDbConnection('aaa','bbb','ccc'); + return $conn; + } +} +</com:TTextHighlighter> + + +<h3 class="prado-specific">Używanie <tt>application.xml</tt> w frameworku Prado</h3> +<div class="prado-specific"> +<p id="690488" class="block-content"> + Domyślne połączenie z bazą może zostać również skonfigurowane używając tagu <tt><module></tt> + w pliku <a href="?page=Configurations.AppConfig">application.xml</a> + lub <a href="?page=Configurations.PageConfig">config.xml</a> następująco: +<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_690151"> +<modules> + <module class="System.Data.ActiveRecord.TActiveRecordConfig" EnableCache="true"> + <database ConnectionString="pgsql:host=localhost;dbname=test" + Username="dbuser" Password="dbpass" /> + </module> +</modules> +</com:TTextHighlighter> +<div class="tip"><b class="note">Wskazówka:</b> + Atrybut <tt>EnableCache</tt> gdy ustawiony na "true" będzie keszował metadane tablicy, to oznacza, że nazwy kolumn, indeksy i ograniczenia (ang. constraints) + są zapisywane w keszu i używane ponownie. Musisz wyczyścić lub wyłączyć kesz jeśli chcesz zobaczyć wprowadzone zmiany do definicji twoich tablic. + <a href="?page=Advanced.Performance#6402">Moduł keszowania</a> musi być również zdefiniowany dla keszu by zadziałał. +</div> +</p> + +<p id="690489" class="block-content">Do właściwość <tt>ConnectionID</tt> może zostać przypisana wartość ID z konfiguracji z innego modułu + <tt>TDataSourceConfig</tt>. To pozwala uyżywać to połączenie z bazą danych w innych modułach, takich jak <a href="?page=Database.SqlMap">SqlMap</a> (mapa SQL). +<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_690152"> +<modules> + <module class="System.Data.TDataSourceConfig" id="db1"> + <database ConnectionString="pgsql:host=localhost;dbname=test" + Username="dbuser" Password="dbpass" /> + </module> + + <module class="System.Data.ActiveRecord.TActiveRecordConfig" + ConnectionID="db1" EnableCache="true" /> + + <module class="System.Data.SqlMap.TSqlMapConfig" + ConnectionID="db1" ... /> +</modules> +</com:TTextHighlighter> +</p> +</div> + +<h2 id="138050">Ładowanie danych z tablicy</h2> +<p id="690490" class="block-content"> + Klasa <tt>TActiveRecord</tt> dostarcza wielu wygodnych metod do wyszukiwania rekordów z bazy danych. + Najprostszym jest znajdowanie jednego rekordu poprzez dopasowanie klucza głównego lub klucza złożonego (ang. composite key) + (klucz główny skłądający się z wielu kolumn). + Zobacz <com:DocLink ClassPath="System.Data.ActiveRecord.TActiveRecord" /> by dowiedzieć się więcej. +</p> + +<div class="info"><b class="note">Info:</b> +Wszystkie metody wyszukujące, które mogą zwrócić tylko 1 rekord zwrócą <tt>null</tt> jeśli nie znajdą pasujących danych. +Wszystkie metody wyszukujące, które zwracają tablicę rekordów zwrócą pustą tablicęm jeśli nie znajdą pasujących danych. +</div> + + <h3 id="138055"><tt>findByPk()</tt></h3> + <p id="690491" class="block-content">Znajduje jeden rekord używając klucza głównego lub klucza złożonego. +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690153"> +$finder = UserRecord::finder(); +$user = $finder->findByPk($primaryKey); + +//kiedy tablica używa klucza złożonego +$record = $finder->findByPk($key1, $key2, ...); +$record = $finder->findByPk(array($key1, $key2,...)); +</com:TTextHighlighter> +</p> + + <h3 id="138056"><tt>findAllByPks()</tt></h3> + <p id="690492" class="block-content">Znajduje wiele rekordów używając listy kluczy głównych lub kluczy złożonych. +Co następuje jest odpowiednie dla kluczów głównych (klucz główny składa się tylko z jednego pola/kolumny) +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690154"> +$finder = UserRecord::finder(); +$users = $finder->findAllByPks($key1, $key2, ...); +$users = $finder->findAllByPks(array($key1, $key2, ...)); +</com:TTextHighlighter> +Co następuje jest odpowiednie dla kluczów złożonych: +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690155"> +//kiedy tablica używa klucza złożonego +$record = $finder->findAllByPks(array($key1, $key2), array($key3, $key4), ...); + +$keys = array( array($key1, $key2), array($key3, $key4), ... ); +$record = $finder->findAllByPks($keys); +</com:TTextHighlighter> + + +<h3 id="138057"><tt>find()</tt></h3> +<p id="690493" class="block-content">Znajduj <b>pojedyńczy rekord</b>, który spełnia kryteria. Kryteria mogą być częściowym łąńcuchem SQL lub obiektem <tt>TActiveRecordCriteria</tt></p> +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690156"> +$finder = UserRecord::finder(); + +//:name oraz :pass są pojemnikami dla konkretnych wartości $name oraz $pass +$finder->find('username = :name AND password = :pass', + array(':name'=>$name, ':pass'=>$pass)); + +//użycie znaków zastępczych +$finder->find('username = ? AND password = ?', array($name, $pass)); +//jak wyżej +$finder->find('username = ? AND password = ?', $name, $pass); + +//$criteria są typu TActiveRecordCriteria +$finder->find($criteria); //drugi parametr dla find() jest zignorowany. +</com:TTextHighlighter> + +<p id="690494" class="block-content">Klasa <tt>TActiveRecordCriteria</tt> ma następujące właściwości: +</p> + <ul id="u2" class="block-content"> + <li><tt>Parameters</tt> -- pary wartość nazwa parametru.</li> + <li><tt>OrdersBy</tt> -- nazwa kolumny i sortowanie par</li> + <li><tt>Condition</tt> -- część WHERE zapytania SQL</li> + <li><tt>Limit</tt> -- maksymalna ilość rekordów, któe zostaną zwrócone.</li> + <li><tt>Offset</tt> -- ofset rekordów w tablicy.</li> + </ul> + +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690157"> +$criteria = new TActiveRecordCriteria; +$criteria->Condition = 'username = :name AND password = :pass'; +$criteria->Parameters[':name'] = 'admin'; +$criteria->Parameters[':pass'] = 'prado'; +$criteria->OrdersBy['level'] = 'desc'; +$criteria->OrdersBy['name'] = 'asc'; +$criteria->Limit = 10; +$criteria->Offset = 20; +</com:TTextHighlighter> + +<div class="note"><b class="note">Note:</b> +For MSSQL and when <tt>Limit</tt> and <tt>Offset</tt> are positive integer values. The +actual query to be executed is modified by the +<com:DocLink ClassPath="System.Data.ActiveRecord.Common.Mssql.TMssqlCommandBuilder" +Text="TMssqlCommandBuilder" +/> +class according to +<a href="http://troels.arvin.dk/db/rdbms/#select-limit-offset">http://troels.arvin.dk/db/rdbms/</a> +to emulate the <tt>Limit</tt> and <tt>Offset</tt> conditions. +</div> + +<h3 id="138058"><tt>findAll()</tt></h3> +<p id="690495" class="block-content">Same as <tt>find()</tt> but returns an array of objects.</p> + +<h3 id="138059"><tt>findBy*()</tt> and <tt>findAllBy*()</tt></h3> +<p id="690496" class="block-content">Dynamic find method using parts of the method name as search criteria. +Method names starting with <tt>findBy</tt> return 1 record only +and method names starting with <tt>findAllBy</tt> return an array of records. +The condition is taken as part of the method name after <tt>findBy</tt> or <tt>findAllBy</tt>. + +The following blocks of code are equivalent: +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690158"> +$finder->findByName($name) +$finder->find('Name = ?', $name); +</com:TTextHighlighter> + +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690159"> +$finder->findByUsernameAndPassword($name,$pass); +$finder->findBy_Username_And_Password($name,$pass); +$finder->find('Username = ? AND Password = ?', $name, $pass); +</com:TTextHighlighter> + +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690160"> +$finder->findAllByAge($age); +$finder->findAll('Age = ?', $age); +</com:TTextHighlighter> + +<div class="tip"><b class="note">Tip:</b> +You may also use a combination of <tt>AND</tt> and <tt>OR</tt> as a condition in the dynamic methods. +</div> + +<h3 id="138060"><tt>findBySql()</tt> and <tt>findAllBySql()</tt></h3> +<p id="690497" class="block-content">Finds records using full SQL where <tt>findBySql()</tt> +return an Active Record and <tt>findAllBySql()</tt>returns an array of record objects. +For each column returned, the corresponding Active Record class must define a member variable or +property for each corresponding column name. +<com:TTextHighlighter Language="php" CssClass="source block-content"> +class UserRecord2 extends UserRecord +{ + public $another_value; +} +$sql = "SELECT users.*, 'hello' as another_value FROM users"; +$users = TActiveRecord::finder('UserRecord2')->findAllBySql($sql); +</com:TTextHighlighter> +</p> +<h3 id="138061"><tt>count()</tt></h3> +<p id="690498" class="block-content">Find the number of matchings records, accepts same parameters as the <tt>findAll()</tt> method.</p> + +<h2 id="138051">Inserting and updating records</h2> +<p id="690499" class="block-content"> +Add a new record using TActiveRecord is very simple, just create a new Active +Record object and call the <tt>save()</tt> method. E.g. +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690161"> +$user1 = new UserRecord(); +$user1->username = "admin"; +$user1->email = "admin@example.com"; +$user1->save(); //insert a new record + +$data = array('username'=>'admin', 'email'=>'admin@example.com'); +$user2 = new UserRecord($data); //create by passing some existing data +$user2->save(); //insert a new record +</com:TTextHighlighter> +<div class="tip"><b class="note">Tip:</b> +The objects are update with the primary key of those the tables that contains +definitions that automatically creates a primary key for the newly insert records. +For example, if you insert a new record into a MySQL table that has columns +defined with "autoincrement", the Active Record objects will be updated with the new +incremented value.</div> + +<p id="690500" class="block-content"> +To update a record in the database, just change one or more properties of +the Active Record object that has been loaded from the database and then +call the <tt>save()</tt> method. + +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690162"> +$user = UserRecord::finder()->findByName('admin'); +$user->email="test@example.com"; //change property +$user->save(); //update it. +</com:TTextHighlighter> +</p> + +<p id="710012" class="block-content"> +Active Record objects have a simple life-cycle illustrated in the following diagram. +</p> +<img src=<%~ object_states.png %> alt="Active Records Life Cycle" id="fig:cycle.png" class="figure"/> +<p id="690501" class="block-content"> +We see that new TActiveRecord objects are created by either using one of the <tt>find*()</tt> +methods or using creating a new instance by using PHP's <tt>new</tt> keyword. Objects +created by a <tt>find*()</tt> method starts with <tt>clean</tt> state. New instance of +TActiveRecord created other than by a <tt>find*()</tt> method starts with <tt>new</tt> state. +Whenever you +call the <tt>save()</tt> method on the TActiveRecord object, the object enters the <tt>clean</tt> +state. Objects in the <tt>clean</tt> becomes <tt>dirty</tt> whenever one of more of its +internal states are changed. Calling the <tt>delete()</tt> method on the object +ends the object life-cycle, no further actions can be performed on the object. +</p> + +<h2 id="138052">Deleting existing records</h2> +<p id="690502" class="block-content"> + To delete an existing record that is already loaded, just call the <tt>delete()</tt> method. + You can also delete records in the database by primary keys without + loading any records using the <tt>deleteByPk()</tt> method (and equivalently the <tt>deleteAllByPks()</tt> method). + For example, to delete one or several records with tables using one or more primary keys. +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690163"> +$finder->deleteByPk($primaryKey); //delete 1 record +$finder->deleteAllByPks($key1,$key2,...); //delete multiple records +$finder->deleteAllByPks(array($key1,$key2,...)); //delete multiple records +</com:TTextHighlighter> + +<p id="690503" class="block-content"> +For composite keys (determined automatically from the table definitions): +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690164"> +$finder->deleteByPk(array($key1,$key2)); //delete 1 record + +//delete multiple records +$finder->deleteAllByPks(array($key1,$key2), array($key3,$key4),...); + +//delete multiple records +$finder->deleteAllByPks(array( array($key1,$key2), array($key3,$key4), .. )); +</com:TTextHighlighter> + +<h3 id="138052a"><tt>deleteAll()</tt> and <tt>deleteBy*()</tt></h3> +<p id="690502a" class="block-content"> +To delete by a criteria, use <tt>deleteAll($criteria)</tt> and <tt>deleteBy*()</tt> +with similar syntax to <tt>findAll($criteria)</tt> and <tt>findAllBy*()</tt> as +described above. +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690163a"> +//delete all records with matching Name +$finder->deleteAll('Name = ?', $name); +$finder->deleteByName($name); + +//delete by username and password +$finder->deleteBy_Username_And_Password($name,$pass); +</com:TTextHighlighter> + +<h2 id="138053">Transactions</h2> +<p id="690504" class="block-content">All Active Record objects contain the property <tt>DbConnection</tt> + that can be used to obtain a transaction object. +<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_690165"> +$finder = UserRecord::finder(); +$finder->DbConnection->Active=true; //open if necessary +$transaction = $finder->DbConnection->beginTransaction(); +try +{ + $user = $finder->findByPk('admin'); + $user->email = 'test@example.com'; //alter the $user object + $user->save(); + $transaction->commit(); +} +catch(Exception $e) // an exception is raised if a query fails +{ + $transaction->rollBack(); +} +</com:TTextHighlighter> + +<h2 id="142011">Events</h2> +<p id="710013" class="block-content"> +The TActiveRecord offers two events, <tt>OnCreateCommand</tt> and <tt>OnExecuteCommand</tt>. +</p> + +<p id="710014" class="block-content">The <tt>OnCreateCommand</tt> event is raised when a command is prepared and +parameter binding is completed. The parameter object is <tt>TDataGatewayEventParameter</tt> of which the +<tt>Command</tt> property can be inspected to obtain the SQL query to be executed. +</p> + +<p id="710015" class="block-content"> +The <tt>OnExecuteCommand</tt> event is raised when a command is executed and the +result from the database was returned. The parameter object is <tt>TDataGatewayResultEventParameter</tt> +of which the <tt>Result</tt> property contains the data return from the database. +The data returned can be changed by setting the <tt>Result</tt> property. +</p> + +<h3 id="142016">Logging Example</h3> +<p id="710016" class="block-content">Using the <tt>OnExecuteCommand</tt> we can attach an event handler to log +the entire SQL query executed for a given TActiveRecord class or instance. For example, we define +a base class and override either the <tt>getDbConnection()</tt> or the constructor. +</p> + +<com:TTextHighlighter Language="php" CssClass="source block-content"> +class MyDb1Record extends TActiveRecord +{ + public function getDbConnection() + { + static $conn; + if($conn===null) + { + $conn = new TDbConnection('xxx','yyy','zzz'); + $this->OnExecuteCommand[] = array($this,'logger'); + } + return $conn; + } + public function logger($sender,$param) + { + var_dump($param->Command->Text); + } +} +//alternatively as per instance of per finder object +function logger($sender,$param) +{ + var_dump($param->Command->Text); +} +TActiveRecord::finder('MyRecord')->OnExecuteCommand[] = 'logger'; +$obj->OnExecuteCommand[] = array($logger, 'log'); //any valid PHP callback. +</com:TTextHighlighter> + +<h1 id="ar_relations">Active Record Relationships</h1> +<com:SinceVersion Version="3.1rc1" /> +<p id="690504a" class="block-content"> +The Prado Active Record implementation supports the foreign key mappings for database +that supports foreign key constraints. For Active Record relationships to function the +underlying database must support foreign key constraints (e.g. MySQL using InnoDB). +</p> + +<p id="710017" class="block-content"> +In the following sections we will consider the following table relationships between +<tt>Teams</tt>, <tt>Players</tt>, <tt>Skills</tt> and <tt>Profiles</tt>. +</p> +<img src=<%~ ar_relations.png %> class="figure" /> + + +<p id="710018" class="block-content">The goal is to obtain object models that represent to some degree the entity +relationships in the above figure. +</p> + +<img src=<%~ ar_objects.png %> class="figure" /> + +<p class="block-content"> +There is a mismatch between relationships with objects and table relationships. +First there's a difference in representation. Objects handle links by storing references +that are held by the runtime memory-managed environment. Relational databases handle +links by forming a key into another table. Second, objects can easily use collections +to handle multiple references from a single field, while normalization forces +all entity relation links to be single valued. This leads to reversals of the data +structure between objects and tables. The approach taken in the Prado Active Record +design is to use the table foreign key constraints to derive object relationships. This implies +that the underlying database must support foreign key constraints. +</p> +<div class="tip"><b class="note">Tip:</b> +For SQLite database, you may create tables that defines the foreign key +constraints such as the example below. However, these constraints are <b>NOT</b> +enforced by the SQLite database itself. +<com:TTextHighlighter Language="sql" CssClass="source block-content"> +CREATE TABLE foo +( + id INTEGER NOT NULL PRIMARY KEY, + id2 CHAR(2) +); +CREATE TABLE bar +( + id INTEGER NOT NULL PRIMARY KEY, + foo_id INTEGER + CONSTRAINT fk_foo_id REFERENCES foo(id) ON DELETE CASCADE +); +</com:TTextHighlighter> +</div> + +<h2 id="142012">Foreign Key Mapping</h2> +<p class="block-content">The entity relationship between the <tt>Teams</tt> and <tt>Players</tt> table is what is known +as an 1-M relationship. That is, one Team may contain 0 or more Players. In terms of +object relationships, we say that a <tt>TeamRecord</tt> object <b>has many</b> <tt>PlayerRecord</tt> objects. +(Notice the reversal of the direction of relationships between tables and objects.) +</p> + +<h3 id="142017">Has Many Relationship</h3> +<p id="710020" class="block-content"> +We model the <tt>Team</tt> object as the following Active Record classes. +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content"> +class TeamRecord extends TActiveRecord +{ + const TABLE='Teams'; + public $name; + public $location; + + public $players=array(); // this declaration is no longer needed since v3.1.2 + + //define the $player member having has many relationship with PlayerRecord + public static $RELATIONS=array + ( + 'players' => array(self::HAS_MANY, 'PlayerRecord', 'team_name'), + ); + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} +</com:TTextHighlighter> +<p id="710021" class="block-content"> +The static <tt>$RELATIONS</tt> property of <tt>TeamRecord</tt> defines that the +property <tt>$players</tt> <b>has many</b> <tt>PlayerRecord</tt>s. Multiple relationships +is permitted by defining each relationship with an entry in the <tt>$RELATIONS</tt> +array where array key for the entry corresponds to the property name. +In <tt>array(self::HAS_MANY, 'PlayerRecord')</tt>, the first element defines the +relationship type, the valid types are <tt>self::HAS_MANY</tt>, <tt>self::HAS_ONE</tt>, +<tt>self::BELONGS_TO</tt> and <tt>self::MANY_TO_MANY</tt>. +The second element is a string <tt>'PlayerRecord'</tt> that corresponds to the +class name of the <tt>PlayerRecord</tt> class. +And the third element 'team_name' refers to the foreign key column in the Players table that +references to the Teams table. +</p> + +<div class="note"><b class="note">Note:</b> +As described in the code comment above, since version <b>3.1.2</b>, related properties no longer +need to be explicitly declared. By default, they will be implicitly declared according to +keys of the <tt>$RELATIONS</tt> array. A major benefit of declared related properties implicitly +is that related objects can be automatically loaded in a lazy way. For example, assume we have +a <tt>TeamRecord</tt> instance <tt>$team</tt>. We can access the players via <tt>$team->players</tt>, +even if we have never issued fetch command for players. If <tt>$players</tt> is explicitly declared, +we will have to use the <tt>with</tt> approach described in the following to fetch the player records. +</div> + +<p id="710022" class="block-content"> +The foreign key constraint of the <tt>Players</tt> table is used to determine the corresponding +<tt>Teams</tt> table's corresponding key names. This is done automatically handled +in Active Record by inspecting the <tt>Players</tt> and <tt>Teams</tt> table definitions. +</p> + +<div class="info"><b class="note">Info:</b> +Since version <b>3.1.2</b>, Active Record supports multiple foreign key +references of the same table. Ambiguity between multiple foreign key references to the same table is +resolved by providing the foreign key column name as the 3rd parameter in the relationship array. +For example, both of the following foreign keys <tt>owner_id</tt> and <tt>reporter_id</tt> +references to the same table defined in <tt>UserRecord</tt>. +<com:TTextHighlighter Language="php" CssClass="source block-content"> +class TicketRecord extends TActiveRecord +{ + public $owner_id; + public $reporter_id; + + public $owner; // this declaration is no longer needed since v3.1.2 + public $reporter; // this declaration is no longer needed since v3.1.2 + + public static $RELATION=array + ( + 'owner' => array(self::BELONGS_TO, 'UserRecord', 'owner_id'), + 'reporter' => array(self::BELONGS_TO, 'UserRecord', 'reporter_id'), + ); +} +</com:TTextHighlighter> +This is applicable to relationships including <tt>BELONGS_TO</tt>, <tt>HAS_ONE</tt> and +<tt>HAS_MANY</tt>. See section <a href="#142021">Self Referenced Association Tables</a> for solving ambiguity of <tt>MANY_TO_MANY</tt> +relationships. +</div> + +<p id="710023" class="block-content">The "has many" relationship is not fetched automatically when you use any of the Active Record finder methods. +You will need to explicitly fetch the related objects as follows. In the code below, both lines +are equivalent and the method names are case insensitive. +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content"> +$team = TeamRecord::finder()->withPlayers()->findAll(); +$team = TeamRecord::finder()->with_players()->findAll(); //equivalent +</com:TTextHighlighter> +<p id="710024" class="block-content"> +The method <tt>with_xxx()</tt> (where <tt>xxx</tt> is the relationship property +name, in this case, <tt>players</tt>) fetches the corresponding PlayerRecords using +a second query (not by using a join). The <tt>with_xxx()</tt> accepts the same +arguments as other finder methods of TActiveRecord, e.g. <tt>with_players('age = ?', 35)</tt>. +</p> + +<div class="note"><b class="note">Note:</b> +It is essential to understand that the related objects are fetched using additional +queries. The first query fetches the source object, e.g. the <tt>TeamRecord</tt> in the above example code. +A second query is used to fetch the corresponding related <tt>PlayerRecord</tt> objects. +The usage of the two query is similar to a single query using Left-Outer join with the +exception that null results on the right table +are not returned. The consequence of using two or more queries is that the aggregates +and other join conditions are not feasible using Active Records. For queries outside the +scope of Active Record the <a href="?page=Database.SqlMap">SqlMap Data Mapper</a> may be considered. +</div> + +<div class="info"><b class="info">Info:</b> +The above <tt>with</tt> approach also works with implicitly declared related properties (introduced +in version 3.1.2). So what is the difference between the <tt>with</tt> approach and the lazy loading +approach? Lazy loading means we issue an SQL query if a related object is initially accessed and not ready, +while the <tt>with</tt> approach queries for the related objects once for all, no matter the related objects +are accessed or not. The lazy loading approach is very convenient since we do not need to explictly +load the related objects, while the <tt>with</tt> approach is more efficient if multiple records are +returned, each with some related objects. +</div> + +<h3 id="142019">Has One Relationship</h3> +<p id="710030" class="block-content">The entity relationship between <tt>Players</tt> and <tt>Profiles</tt> is one to one. That is, +each <tt>PlayerRecord</tt> object <b>has one</b> <tt>ProfileRecord</tt> object (may be none or null). +A <b>has one</b> relationship is nearly identical to a <b>has many</b> relationship with the exception +that the related object is only one object (not a collection of objects). +</p> + +<h3 id="142018">Belongs To Relationship</h3> +<p id="710025" class="block-content">The "has many" relationship in the above section defines a collection of foreign +objects. In particular, we have that a <tt>TeamRecord</tt> has many (zero or more) +<tt>PlayerRecord</tt> objects. We can also add a back pointer by adding a property +in the <tt>PlayerRecord</tt> class that links back to the <tt>TeamRecord</tt> object, +effectively making the association bidirectional. +We say that the <tt>$team</tt> property in <tt>PlayerRecord</tt> class <tt>belongs to</tt> a <tt>TeamRecord</tt> object. +The following code defines the complete <tt>PlayerRecord</tt> class with 3 relationships. +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content"> +class PlayerRecord extends TActiveRecord +{ + const TABLE='Players'; + public $player_id; + public $age; + public $team_name; + + public $team; // this declaration is no longer needed since v3.1.2 + public $skills=array(); // this declaration is no longer needed since v3.1.2 + public $profile; // this declaration is no longer needed since v3.1.2 + + public static $RELATIONS=array + ( + 'team' => array(self::BELONGS_TO, 'TeamRecord', 'team_name'), + 'skills' => array(self::MANY_TO_MANY, 'SkillRecord', 'Player_Skills'), + 'profile' => array(self::HAS_ONE, 'ProfileRecord', 'player_id'), + ); + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} +</com:TTextHighlighter> +<p id="710026" class="block-content"> +The static <tt>$RELATIONS</tt> property of <tt>PlayerRecord</tt> defines that the +property <tt>$team</tt> <b>belongs to</b> a <tt>TeamRecord</tt>. +The <tt>$RELATIONS</tt> array also defines two other relationships that we +shall examine in later sections below. +In <tt>array(self::BELONGS_TO, 'TeamRecord', 'team_name')</tt>, the first element defines the +relationship type, in this case <strong><tt>self::BELONGS_TO</tt></strong>; +the second element is a string <tt>'TeamRecord'</tt> that corresponds to the +class name of the <tt>TeamRecord</tt> class; and the third element 'team_name' refers +to the foreign key of Players referencing Teams. +A player object with the corresponding team object may be fetched as follows. +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content"> +$players = PlayerRecord::finder()->with_team()->findAll(); +</com:TTextHighlighter> + +<p id="710027" class="block-content"> + The method <tt>with_xxx()</tt> (where <tt>xxx</tt> is the relationship property + name, in this case, <tt>team</tt>) fetches the corresponding <tt>TeamRecords</tt> using + a second query (not by using a join). The <tt>with_xxx()</tt> accepts the same +arguments as other finder methods of <tt>TActiveRecord</tt>, e.g. +<tt>with_team('location = ?', 'Madrid')</tt>. +</p> + +<div class="tip"><b class="note">Tip:</b> +Additional relationships may be fetched by chaining the <tt>with_xxx()</tt> together as the following +example demonstrates. +<com:TTextHighlighter Language="php" CssClass="source block-content"> +$players = PlayerRecord::finder()->with_team()->with_skills()->findAll(); +</com:TTextHighlighter> +Each <tt>with_xxx()</tt> method will execute an additional SQL query. Every +<tt>with_xxx()</tt> accepts arguments similar to those in the <tt>findAll()</tt> method and is only +applied to that particular relationship query. +</div> + +<p id="710028" class="block-content">The "belongs to" relationship of <tt>ProfileRecord</tt> class is defined similarly.</p> +<com:TTextHighlighter Language="php" CssClass="source block-content"> +class ProfileRecord extends TActiveRecord +{ + const TABLE='Profiles'; + public $player_id; + public $salary; + + public $player; // this declaration is no longer needed since v3.1.2 + + public static $RELATIONS=array + ( + 'player' => array(self::BELONGS_TO, 'PlayerRecord'), + ); + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} +</com:TTextHighlighter> + +<p id="710029" class="block-content">In essence, there exists a "<b>belongs to</b>" relationship for objects corresponding to +entities that has column which are foreign keys. In particular, we see that +the <tt>Profiles</tt> table has a foreign key constraint on the column <tt>player_id</tt> +that relates to the <tt>Players</tt> table's <tt>player_id</tt> column. Thus, the <tt>ProfileRecord</tt> +object has a property (<tt>$player</tt>) that <b>belongs to</b> a <tt>PlayerRecord</tt> object. +Similarly, the <tt>Players</tt> table has a foreign key constraint on the column <tt>team_name</tt> that relates to the +<tt>Teams</tt> table's <tt>name</tt> column. +Thus, the <tt>PlayerRecord</tt> object has a property (<tt>$team</tt>) that <b>belongs to</b> a +<tt>TeamRecord</tt> object. +</p> + +<h3 id="142020">Parent Child Relationships</h3> +<p id="710031" class="block-content">A parent child relationship can be defined using a combination of <tt>has many</tt> and <tt>belongs to</tt> +relationship that refers to the same class. The following example shows a parent children relationship between +"categories" and a "parent category". +</p> + +<com:TTextHighlighter Language="php" CssClass="source block-content"> +class Category extends TActiveRecord +{ + public $cat_id; + public $category_name; + public $parent_cat_id; + + public $parent_category; // this declaration is no longer needed since v3.1.2 + public $child_categories=array(); // this declaration is no longer needed since v3.1.2 + + public static $RELATIONS=array + ( + 'parent_category' => array(self::BELONGS_TO, 'Category', 'parent_cat_id'), + 'child_categories' => array(self::HAS_MANY, 'Category', 'parent_cat_id'), + ); +} +</com:TTextHighlighter> + +<h3>Query Criteria for Related Objects</h3> +<p> +In the above, we show that an Active Record object can reference to its related objects by +declaring a static class member $RELATIONS which specifies a list of relations. Each relation +is specified as an array consisting of three elements: relation type, related AR class name, +and the foreign key(s). For example, we use <tt>array(self::HAS_MANY, 'PlayerRecord', 'team_name')</tt> +to specify the players in a team. There are two more optional elements that can be specified +in this array: query condition (the fourth element) and parameters (the fifth element). +They are used to control how to query for the related objects. For example, if we want to obtain +the players ordered by their age, we can specify <tt>array(self::HAS_MANY, 'PlayerRecord', 'team_name', 'ORDER BY age')</tt>. +If we want to obtain players whose age is smaller than 30, we could use +<tt>array(self::HAS_MANY, 'PlayerRecord', 'team_name', 'age<:age', array(':age'=>30))</tt>. In general, +these two additional elements are similar as the parameters passed to the <tt>find()</tt> method in AR. +</p> + + + +<h2 id="142013">Association Table Mapping</h2> +<p id="710032" class="block-content"> +Objects can handle multivalued fields quite easily by using collections as field values. +Relational databases don't have this feature and are constrained to single-valued fields only. +When you're mapping a one-to-many association you can handle this using <b>has many</b> relationships, +essentially using a foreign key for the single-valued end of the association. +But a many-to-many association can't do this because there is no single-valued end to +hold the foreign key. +</p> +<p id="710033" class="block-content"> +The answer is the classic resolution that's been used by relational data people +for decades: create an extra table (an association table) to record the relationship. +The basic idea is using an association table to store the association. This table +has only the foreign key IDs for the two tables that are linked together, it has one +row for each pair of associated objects. +</p> +<p id="710034" class="block-content"> +The association table has no corresponding in-memory object and its primary key is the +compound of the two primary keys of the tables that are associated. +In simple terms, to load data from the association table you perform two queries (in general, it may also be achieved using one query consisting of joins). +Consider loading the <tt>SkillRecord</tt> collection for a list <tt>PlayerRecord</tt> objects. +In this case, you do queries in two stages. +The first stage queries the <tt>Players</tt> table to find all the rows of the players you want. +The second stage finds the <tt>SkillRecord</tt> object for the related player ID for each row +in the <tt>Player_Skills</tt> association table using an inner join. +</p> + +<p id="710035" class="block-content">The Prado Active Record design implements the two stage approach. For the +<tt>Players</tt>-<tt>Skills</tt> M-N (many-to-many) entity relationship, we +define a <b>many-to-many</b> relationship in the <tt>PlayerRecord</tt> class and +in addition we may define a <b>many-to-many</b> relationship in the <tt>SkillRecord</tt> class as well. +The following sample code defines the complete <tt>SkillRecord</tt> class with a +many-to-many relationship with the <tt>PlayerRecord</tt> class. (See the <tt>PlayerRecord</tt> +class definition above to the corresponding many-to-many relationship with the <tt>SkillRecord</tt> class.) +</p> + +<com:TTextHighlighter Language="php" CssClass="source block-content"> +class SkillRecord extends TActiveRecord +{ + const TABLE='Skills'; + public $skill_id; + public $name; + + public $players=array(); // this declaration is no longer needed since v3.1.2 + + public static $RELATIONS=array + ( + 'players' => array(self::MANY_TO_MANY, 'PlayerRecord', 'Player_Skills'), + ); + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } +} +</com:TTextHighlighter> + +<p id="710036" class="block-content"> +The static <tt>$RELATIONS</tt> property of SkillRecord defines that the +property <tt>$players</tt> has many <tt>PlayerRecord</tt>s via an association table '<tt>Player_Skills</tt>'. +In <tt>array(self::MANY_TO_MANY, 'PlayerRecord', 'Player_Skills')</tt>, the first element defines the +relationship type, in this case <strong><tt>self::MANY_TO_MANY</tt></strong>, +the second element is a string <tt>'PlayerRecord'</tt> that corresponds to the +class name of the <tt>PlayerRecord</tt> class, and the third element is the name +of the association table name. +</p> + +<div class="note"><b class="note">Note:</b> +Prior to version <b>3.1.2</b> (versions up to 3.1.1), the many-to-many relationship was +defined using <tt>self::HAS_MANY</tt>. For version <b>3.1.2</b> onwards, this must be changed +to <tt>self::MANY_TO_MANY</tt>. This can be done by searching for the <tt>HAS_MANY</tt> in your +source code and carfully changing the appropriate definitions. +</div> + +<p id="710037" class="block-content"> +A list of player objects with the corresponding collection of skill objects may be fetched as follows. +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content"> +$players = PlayerRecord::finder()->withSkills()->findAll(); +</com:TTextHighlighter> +<p id="710038" class="block-content"> +The method <tt>with_xxx()</tt> (where <tt>xxx</tt> is the relationship property +name, in this case, <tt>Skill</tt>) fetches the corresponding <tt>SkillRecords</tt> using +a second query (not by using a join). The <tt>with_xxx()</tt> accepts the same +arguments as other finder methods of <tt>TActiveRecord</tt>. +</p> + +<h3 id="142021">Self Referenced Association Tables</h3> +<p id="710039" class="block-content"> +For self referenced association tables, that is, the association points to the same +table. For example, consider the <tt>items</tt> table with M-N related +item via the <tt>related_items</tt> association table. The syntax in the following +example is valid for a PostgreSQL database. For other database, consult their respective documentation for +defining the foreign key constraints. +<com:TTextHighlighter Language="sql" CssClass="source block-content"> +CREATE TABLE items +( + "item_id" SERIAL, + "name" VARCHAR(128) NOT NULL, + PRIMARY KEY("item_id") +); +CREATE TABLE "related_items" +( + "item_id" INTEGER NOT NULL, + "related_item_id" INTEGER NOT NULL, + CONSTRAINT "related_items_pkey" PRIMARY KEY("item_id", "related_item_id"), + CONSTRAINT "related_items_item_id_fkey" FOREIGN KEY ("item_id") + REFERENCES "items"("item_id") + ON DELETE CASCADE + ON UPDATE NO ACTION + NOT DEFERRABLE, + CONSTRAINT "related_items_related_item_id_fkey" FOREIGN KEY ("related_item_id") + REFERENCES "items"("item_id") + ON DELETE CASCADE + ON UPDATE NO ACTION + NOT DEFERRABLE +); +</com:TTextHighlighter> + +<p id="710040" class="block-content">The association table name in third element of the relationship array may +contain the foreign table column names. The columns defined in the association +table must also be defined in the record class (e.g. the <tt>$related_item_id</tt> property +corresponds to the <tt>related_item_id</tt> column in the <tt>related_items</tt> table). +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content"> +class Item extends TActiveRecord +{ + const TABLE="items"; + public $item_id; + public $details; + + //additional foreign item id defined in the association table + public $related_item_id; + public $related_items=array(); // this declaration is no longer needed since v3.1.2 + + public static $RELATIONS=array + ( + 'related_items' => array(self::MANY_TO_MANY, + 'Item', 'related_items.related_item_id'), + ); +} +</com:TTextHighlighter> +<div class="tip"><b class="note">Tip:</b> +Compound keys in the foreign table can +be specified as comma separated values between brackets. E.g. +<tt>'related_items.(id1,id2)'</tt>. +</div> + +<!--- +<h2 id="142014">Adding/Removing/Updating Related Objects</h2> + +<p id="710041" class="block-content">Related objects can be simply inserted/updated by first adding those related objects to +the current source object (i.e. the object currently been worked on) and then call +the <tt>save()</tt> method on the source object. The related object's references +and the association reference (if required) will be added and/or updated. +For example, to add two new players to the team (assuming that 'Team A' exists), we can simply do the following. +</p> + +<com:TTextHighlighter Language="php" CssClass="source block-content"> +$team = TeamRecord::finder()->findByPk('Team A'); +$team->players[] = new PlayerRecord(array('age'=>20)); +$team->players[] = new PlayerRecord(array('age'=>25)); +$team->save(); +</com:TTextHighlighter> +<p id="710042" class="block-content"> +Since the <tt>TeamRecord</tt> class contains a <b>has many</b> relationship with the <tt>PlayerRecord</tt>, +then saving a <tt>TeamRecord</tt> object will also update the corresponding foreign objects in <tt>$players</tt> array. +That is, the objects in <tt>$players</tt> are inserted/updated in the database and the +<tt>$team_name</tt> property of those objects will contain the foreign key value that corresponds to the <tt>$team</tt> object's primary key value. +</p> + +<p id="710043" class="block-content">To delete a particular foreign object (or any Active Record object), simply call +the object's <tt>delete()</tt> method. You may setup the database table's foreign key constraints such that +when deleting a particular data in the database it will delete the referenced data as well (it may also be achieved using database +triggers). E.g. such as having a "<tt>ON DELETE CASCADE</tt>" constraint. +Deleting foreign objects by either setting the property value to null or removing the object from an array will <b>NOT</b> +remove the corresponding data in the database. +</p> + +<p id="710044" class="block-content">To remove associations for the many-to-many relationships via an association table, an Active Record +that corresponds to the association table can be used. Then the association can be removed by calling the <tt>deleteByPk()</tt> method, for example: +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content"> +PlayerSkillAssocation::finder()->deleteByPk(array('fk1','fk2')); +//where 'fk1' is the primary key value of a player +// and 'fk2' is the primary key value of a skill +</com:TTextHighlighter> +---> + +<h2 id="142015">Lazy Loading Related Objects</h2> + +<div class="note"><b class="note">Note:</b> +Implicitly declared related properties introduced in version 3.1.2 automatically have lazy +loading feature. Therefore, the lazy loading technique described in the following is no longer +needed in most of the cases, unless you want to manipulate the related objects through getter/setter. +</div> + +<p id="710045" class="block-content">Using the <tt>with_xxx()</tt> methods will load the relationship record on demand. Retrieving the +related record using lazy loading (that is, only when those related objects are accessed) can be +achieved by using a feature of the <tt>TComponent</tt> that provides accessor methods. In particular, +we define a pair of getter and setter methods where the getter method will retrieve the relationship +conditionally. The following example illustrates that the <tt>PlayerRecord</tt> can retrieve its +<tt>$skills</tt> foreign objects conditionally. +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content"> +class PlayerRecord extends BaseFkRecord +{ + //... other properties and methods as before + + private $_skills; //change to private and default as null + + public function getSkills() + { + if($this->_skills===null && $this->player_id !==null) + { + //lazy load the skill records + $this->setSkills($this->withSkills()->findByPk($this->player_id)->skills); + } + else if($this->_skills===null) + { + //create new TList; + $this->setSkills(new TList()); + } + + return $this->_skills; + } + + public function setSkills($value) + { + $this->_skills = $value instanceof TList ? $value : new TList($value); + } +} +</com:TTextHighlighter> +<p id="710046" class="block-content">We first need to change the <tt>$skills=array()</tt> declaration to a private property +<tt>$_skills</tt> (notice the underscore) and set it to null instead. This allows us +to define the <tt>skills</tt> property using getter/setter methods +(see <a href="?page=Fundamentals.Components">Components</a> for details). The <tt>getSkills()</tt> +getter method for the <tt>skills</tt> property will lazy load the corresponding skills foreign record +when it is used as follows. Notice that we only do a lazy load when its <tt>$player_id</tt> is +not null (that is, when the record is already fetched from the database or player id was already set). +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content"> +$player = PlayerRecord::finder()->findByPk(1); +var_dump($player->skills); //lazy load it on first access +var_dump($player->skills[0]); //already loaded skills property +$player->skills[] = new SkillRecord(); //add skill +</com:TTextHighlighter> + +<p id="710047" class="block-content">The <tt>setSkills()</tt> ensures that the <tt>skills</tt> property will always be a TList. +Using a TList allows us to set the elements of the <tt>skills</tt> property as if they were +arrays. E.g. <tt>$player->skills[] = new SkillRecord()</tt>. If <tt>array</tt> was used, a PHP error +will be thrown. +</p> + +<h2>Column Mapping</h2> +<p> +Since v3.1.1, Active Record starts to support column mapping. Column mapping allows developers +to address columns in Active Record using a more consistent naming convention. In particular, +using column mapping, one can access a column using whatever name he likes, rather than limited by +the name defined in the database schema. +</p> +<p> +To use column mapping, declare a static array named <tt>COLUMN_MAPPING</tt> in the Active Record class. +The keys of the array are column names (called <i>physical column names</i>) as defined in the database +schema, while the values are corresponding property names (called <i>logical column names</i>) defined +in the Active Record class. The property names can be either public class member variable names or +component property names defined via getters/setters. If a physical column name happens to be the same +as the logical column name, they do not need to be listed in <tt>COLUMN_MAPPING</tt>. +</p> +<com:TTextHighlighter Language="php" CssClass="source block-content"> +class UserRecord extends TActiveRecord +{ + const TABLE='users'; + public static $COLUMN_MAPPING=array + ( + 'user_id'=>'id', + 'email_address'=>'email', + 'first_name'=>'firstName', + 'last_name'=>'lastName', + ); + public $id; + public $username; // the physical and logical column names are the same + public $email; + public $firstName; + public $lastName; + //.... +} +</com:TTextHighlighter> +<p> +With the above column mapping, we can address <tt>first_name</tt> using <tt>$userRecord->firstName</tt> +instead of <tt>$userRecord->first_name</tt>. This helps separation of logic and model. +</p> + +<h2 id="138054">References</h2> +<ul id="u3" class="block-content"> + <li>Fowler et. al. <i>Patterns of Enterprise Application Architecture</i>, + Addison Wesley, 2002.</li> + <li>B. Venners with B. Eckel. <i><a href="http://www.artima.com/intv/abstract3.html">Inappropriate Abstractions - A Conversation with Anders Hejlsberg, Part VI.</a></i> + Artima Developer, 2003. + </li> + +</ul> + +<div class="last-modified">$Id$</div></com:TContent>
\ No newline at end of file diff --git a/demos/quickstart/protected/pages/Database/pl/ar_objects.png b/demos/quickstart/protected/pages/Database/pl/ar_objects.png Binary files differnew file mode 100644 index 00000000..ac33b88b --- /dev/null +++ b/demos/quickstart/protected/pages/Database/pl/ar_objects.png diff --git a/demos/quickstart/protected/pages/Database/pl/ar_relations.png b/demos/quickstart/protected/pages/Database/pl/ar_relations.png Binary files differnew file mode 100644 index 00000000..48e29f48 --- /dev/null +++ b/demos/quickstart/protected/pages/Database/pl/ar_relations.png diff --git a/demos/quickstart/protected/pages/Database/pl/diagram.png b/demos/quickstart/protected/pages/Database/pl/diagram.png Binary files differnew file mode 100644 index 00000000..0a0ca73d --- /dev/null +++ b/demos/quickstart/protected/pages/Database/pl/diagram.png diff --git a/demos/quickstart/protected/pages/Database/pl/object_states.png b/demos/quickstart/protected/pages/Database/pl/object_states.png Binary files differnew file mode 100755 index 00000000..db194783 --- /dev/null +++ b/demos/quickstart/protected/pages/Database/pl/object_states.png diff --git a/demos/quickstart/protected/pages/Database/pl/sqlmap_active_record.png b/demos/quickstart/protected/pages/Database/pl/sqlmap_active_record.png Binary files differnew file mode 100755 index 00000000..6d958d33 --- /dev/null +++ b/demos/quickstart/protected/pages/Database/pl/sqlmap_active_record.png diff --git a/demos/quickstart/protected/pages/pl/Construction.page b/demos/quickstart/protected/pages/pl/Construction.page new file mode 100644 index 00000000..bf96f99c --- /dev/null +++ b/demos/quickstart/protected/pages/pl/Construction.page @@ -0,0 +1,5 @@ +<com:TContent ID="body">
+
+Przepraszamy, ta strona jest w trakcie tworzenia. Proszę sprawdź później.
+
+<div class="last-modified">$Id: Construction.page 1650 2007-01-24 06:55:32Z wei $</div></com:TContent>
\ No newline at end of file diff --git a/demos/quickstart/protected/pages/pl/Search.page b/demos/quickstart/protected/pages/pl/Search.page new file mode 100644 index 00000000..ab0515eb --- /dev/null +++ b/demos/quickstart/protected/pages/pl/Search.page @@ -0,0 +1,29 @@ +<com:TContent ID="body">
+<div class="quicksearch">
+ <div class="search">
+ <com:SearchBox ID="search" />
+ </div>
+
+ <com:TRepeater id="quickstart_results">
+ <prop:HeaderTemplate>
+ <div class="searchHeader">
+ <%# $this->Parent->DataSource->Count %> znalezionych w Quickstart Tutorial.
+ </div>
+ </prop:HeaderTemplate>
+ <prop:ItemTemplate>
+ <div class="searchItem">
+ <div class="searchItemLink">
+ <%# $this->ItemIndex + 1 %>.
+ <a href="<%# $this->DataItem->link %>">
+ <%# $this->DataItem->title %>
+ </a>
+ </div>
+ <p class="searchItemBody"><%# $this->Page->HighlightSearch($this->DataItem->text) %></p>
+ </div>
+ </prop:ItemTemplate>
+ </com:TRepeater>
+ <com:TPanel id="emptyResult" CssClass="empty_search_result" Visible="false">
+ Nie znaleziono żadnych rezultatów dla frazy "<%= htmlentities($this->Page->search->Text) %>".
+ </com:TPanel>
+</div>
+<div class="last-modified">$Id: Search.page 1859 2007-04-10 21:27:01Z xue $</div></com:TContent>
\ No newline at end of file diff --git a/demos/quickstart/protected/pages/pl/ViewSource.page b/demos/quickstart/protected/pages/pl/ViewSource.page new file mode 100644 index 00000000..8d5e0574 --- /dev/null +++ b/demos/quickstart/protected/pages/pl/ViewSource.page @@ -0,0 +1,31 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
+
+<com:THead Title="Podgląd źródeł w PRADO QuickStart">
+<meta http-equiv="content-language" content="en"/>
+</com:THead>
+
+<body>
+<com:TForm>
+<div id="sourceList">
+<table border="0">
+<com:TRepeater ID="SourceList">
+<prop:ItemTemplate>
+<tr>
+ <td align="right"><%# $this->DataItem['type']%>:</td>
+ <td><a href="<%# $this->DataItem['active']?'':$this->DataItem['url']%>"><%# $this->DataItem['name']%></a></td>
+</tr>
+</prop:ItemTemplate>
+</com:TRepeater>
+</table>
+<com:TCheckBox ID="showNumbers" Text="Pokaż numery linii" Checked="true" AutoPostBack="true" />
+</div>
+<div id="sourceView">
+<h3 style="text-align:center"><%= $this->FilePath %></h3>
+<com:TTextHighlighter ID="Highlighter" ShowLineNumbers=<%= $this->showNumbers->Checked %> CssClass="source">
+<com:TLiteral ID="SourceView" />
+</com:TTextHighlighter>
+</div>
+</com:TForm>
+</body>
+</html>
\ No newline at end of file |