I was just going to display the fields in a record and my first thought was to use the relevant class and then just call get-dao and iterate over the slots, something like:
(item (postmodern:get-dao 'countrynotes (parse-integer id))))
However, looking at the countrynotes definition, you quickly notice that it has a foreign key:
; shortened version of the countrynotes class (defclass countrynotes () ( (id :accessor id :col-type int4 :initarg :id ) (country-id :accessor country-id :col-type int4 :initarg :country-id :foreign-key (countries id)) (name :accessor name :col-type varchar :initarg :name ) (updated-at :accessor updated-at :col-type timestamp :initarg :updated-at )) (:metaclass postmodern:dao-class) (:table-name countrynotes)(:keys id))
In displaying the countrynote, I don't really want to show the country-id, I really want to display the name of the country instead, which is another table: (defclass countries () ( (id :accessor id :col-type int4 :initarg :id ) (name :accessor name :col-type varchar :initarg :name ) (updated-at :accessor updated-at :col-type timestamp :initarg :updated-at )) (:metaclass postmodern:dao-class) (:table-name countries)(:keys id))
I can easily write out the full required sql statement, but is there a simple way of just using the dao?
Sabra
Hi Sabra,
There's no elegant provision for implicitly joining things onto DAO objects, but there is a hack. See http://common-lisp.net/project/postmodern/postmodern.html#with-column-writer... . Here's the only that I ever made of it (as far as I know, it's the only use ever made of it at all), with some names replaced to protect all the trade secrets that are obviously present in such a thing:
(defun get-foos-by (condition) (with-column-writers ('actives 'has-active-bars 'shape 'shape) (query-dao 'foo-desc (:select 'foo.* (:as 'shape.name 'shape) (:as (:exists (:select 1 :from 'bar :where (:and (:= 'foo-id 'foo.id) 'active))) 'actives) :from 'foo :left-join 'shape :on (:= 'shape.id 'foo.shape-id) :where (:raw condition)))))
Hope that helps, Marijn
postmodern-devel@common-lisp.net