Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.06 KB | None | 0 0
  1. class DeliveryManifestLines extends Eloquent
  2. {
  3. protected $table = 'manifests';
  4.  
  5. public function sapDelivery()
  6. {
  7. return $this->hasOne('Delivery', 'DocNum', 'sap_delivery');
  8. }
  9.  
  10. }
  11.  
  12.  
  13. class Delivery extends Eloquent
  14. {
  15. protected $connection = 'sap';
  16. protected $table = 'ODLN';
  17. protected $primaryKey = 'DocNum';
  18.  
  19. public function deliveryManifest() {
  20. return $this->belongsTo('DeliveryManifest', 'DocNum', 'sap_delivery');
  21. }
  22.  
  23. public function address()
  24. {
  25. return $this->hasOne('Address', 'Address', 'ShipToCode')->where('CardCode', $this->CardCode)->where('AdresType', 'S');
  26. }
  27.  
  28. public function geolocation()
  29. {
  30. return $this->hasOne('GeoLocation', 'Address', 'ShipToCode')->where('CardCode', $this->CardCode)->where('AdresType', 'S')->where('Lat', '>', 0)->where('Lng', '>', 0);
  31. }
  32. }
  33.  
  34. class Address extends Eloquent
  35. {
  36. protected $connection = 'sap';
  37. protected $table = 'CRD1';
  38. protected $primaryKey = 'Address';
  39.  
  40. public function delivery() {
  41. return $this->belongsTo('Delivery', 'Address', 'ShipToCode');
  42. }
  43.  
  44. }
  45.  
  46. $deliveries = DeliveryManifestLines::with('sapDelivery')->where('manifest_date', $date))->get();
  47.  
  48. foreach ($deliveries as $delivery) {
  49. $delivery->sapDelivery->load('address');
  50. }
  51.  
  52. SELECT * FROM [CRD1] WHERE [CardCode] = 'P437' AND [AdresType] = 'S' AND [CRD1].[Address] IN ('The Pizza Factory (topping)')
  53.  
  54. <?php
  55.  
  56. namespace AppModelsEloquent;
  57.  
  58. use AppModelsEloquentRelationsBelongsToCI;
  59. use AppModelsEloquentRelationsHasManyCI;
  60. use AppModelsEloquentRelationsHasOneCI;
  61. use IlluminateDatabaseEloquentModel;
  62.  
  63.  
  64. abstract class ModelCI extends Model
  65. {
  66. /**
  67. * Define a one-to-many relationship.
  68. *
  69. * @param string $related
  70. * @param string $foreignKey
  71. * @param string $localKey
  72. *
  73. * @return IlluminateDatabaseEloquentRelationsHasMany
  74. */
  75. public function hasManyCI($related, $foreignKey = null, $localKey = null)
  76. {
  77. $foreignKey = $foreignKey ?: $this->getForeignKey();
  78.  
  79. $instance = new $related();
  80.  
  81. $localKey = $localKey ?: $this->getKeyName();
  82.  
  83. return new HasManyCI($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
  84. }
  85.  
  86. /**
  87. * Define a one-to-one relationship.
  88. *
  89. * @param string $related
  90. * @param string $foreignKey
  91. * @param string $localKey
  92. * @return IlluminateDatabaseEloquentRelationsHasOne
  93. */
  94. public function hasOneCI($related, $foreignKey = null, $localKey = null)
  95. {
  96. $foreignKey = $foreignKey ?: $this->getForeignKey();
  97.  
  98. $instance = new $related;
  99.  
  100. $localKey = $localKey ?: $this->getKeyName();
  101.  
  102. return new HasOneCI($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
  103. }
  104.  
  105. /**
  106. * Define an inverse one-to-one or many relationship.
  107. *
  108. * @param string $related
  109. * @param string $foreignKey
  110. * @param string $otherKey
  111. * @param string $relation
  112. * @return IlluminateDatabaseEloquentRelationsBelongsTo
  113. */
  114. public function belongsToCI($related, $foreignKey = null, $otherKey = null, $relation = null)
  115. {
  116. // If no relation name was given, we will use this debug backtrace to extract
  117. // the calling method's name and use that as the relationship name as most
  118. // of the time this will be what we desire to use for the relationships.
  119. if (is_null($relation))
  120. {
  121. list(, $caller) = debug_backtrace(false, 2);
  122.  
  123. $relation = $caller['function'];
  124. }
  125.  
  126. // If no foreign key was supplied, we can use a backtrace to guess the proper
  127. // foreign key name by using the name of the relationship function, which
  128. // when combined with an "_id" should conventionally match the columns.
  129. if (is_null($foreignKey))
  130. {
  131. $foreignKey = snake_case($relation).'_id';
  132. }
  133.  
  134. $instance = new $related;
  135.  
  136. // Once we have the foreign key names, we'll just create a new Eloquent query
  137. // for the related models and returns the relationship instance which will
  138. // actually be responsible for retrieving and hydrating every relations.
  139. $query = $instance->newQuery();
  140.  
  141. $otherKey = $otherKey ?: $instance->getKeyName();
  142.  
  143. return new BelongsToCI($query, $this, $foreignKey, $otherKey, $relation);
  144. }
  145. }
  146.  
  147. <?php namespace AppModelsEloquentRelations;
  148.  
  149.  
  150. use IlluminateDatabaseEloquentModel;
  151. use IlluminateDatabaseEloquentBuilder;
  152. use IlluminateDatabaseQueryExpression;
  153. use IlluminateDatabaseEloquentCollection;
  154. use IlluminateDatabaseEloquentRelationsBelongsTo;
  155.  
  156. class BelongsToCI extends BelongsTo {
  157. /**
  158. * Match the eagerly loaded results to their parents.
  159. *
  160. * @param array $models
  161. * @param IlluminateDatabaseEloquentCollection $results
  162. * @param string $relation
  163. * @return array
  164. */
  165. public function match(array $models, Collection $results, $relation)
  166. {
  167. $foreign = $this->foreignKey;
  168.  
  169. $other = $this->otherKey;
  170.  
  171. // First we will get to build a dictionary of the child models by their primary
  172. // key of the relationship, then we can easily match the children back onto
  173. // the parents using that dictionary and the primary key of the children.
  174. $dictionary = array();
  175.  
  176. foreach ($results as $result)
  177. {
  178. $dictionary[strtolower($result->getAttribute($other))] = $result;
  179. }
  180.  
  181. // Once we have the dictionary constructed, we can loop through all the parents
  182. // and match back onto their children using these keys of the dictionary and
  183. // the primary key of the children to map them onto the correct instances.
  184. foreach ($models as $model)
  185. {
  186. if (isset($dictionary[strtolower($model->$foreign)]))
  187. {
  188. $model->setRelation($relation, $dictionary[strtolower($model->$foreign)]);
  189. }
  190. }
  191.  
  192. return $models;
  193. }
  194.  
  195. }
  196.  
  197. <?php namespace AppModelsEloquentRelations;
  198.  
  199. use IlluminateDatabaseEloquentCollection;
  200. use IlluminateDatabaseEloquentRelationsHasMany;
  201.  
  202. class HasManyCI extends HasMany {
  203. /**
  204. * Build model dictionary keyed by the relation's foreign key.
  205. *
  206. * @param IlluminateDatabaseEloquentCollection $results
  207. * @return array
  208. */
  209. protected function buildDictionary(Collection $results)
  210. {
  211. $dictionary = array();
  212.  
  213. $foreign = $this->getPlainForeignKey();
  214.  
  215. // First we will create a dictionary of models keyed by the foreign key of the
  216. // relationship as this will allow us to quickly access all of the related
  217. // models without having to do nested looping which will be quite slow.
  218. foreach ($results as $result)
  219. {
  220. $dictionary[strtolower($result->{$foreign})][] = $result;
  221. }
  222.  
  223. return $dictionary;
  224. }
  225.  
  226. /**
  227. * Match the eagerly loaded results to their many parents.
  228. *
  229. * @param array $models
  230. * @param IlluminateDatabaseEloquentCollection $results
  231. * @param string $relation
  232. * @param string $type
  233. * @return array
  234. */
  235. protected function matchOneOrMany(array $models, Collection $results, $relation, $type)
  236. {
  237. $dictionary = $this->buildDictionary($results);
  238.  
  239.  
  240. // Once we have the dictionary we can simply spin through the parent models to
  241. // link them up with their children using the keyed dictionary to make the
  242. // matching very convenient and easy work. Then we'll just return them.
  243. foreach ($models as $model)
  244. {
  245. $key = strtolower( $model->getAttribute($this->localKey) );
  246. if (isset($dictionary[$key]))
  247. {
  248. $value = $this->getRelationValue($dictionary, $key, $type);
  249. $model->setRelation($relation, $value);
  250. }
  251. }
  252.  
  253. return $models;
  254. }
  255.  
  256. }
  257.  
  258. <?php namespace AppModelsEloquentRelations;
  259.  
  260. use IlluminateDatabaseEloquentCollection;
  261. use IlluminateDatabaseEloquentRelationsHasOne;
  262.  
  263. class HasOneCI extends HasOne {
  264.  
  265. /**
  266. * Match the eagerly loaded results to their many parents.
  267. *
  268. * @param array $models
  269. * @param IlluminateDatabaseEloquentCollection $results
  270. * @param string $relation
  271. * @param string $type
  272. * @return array
  273. */
  274. protected function matchOneOrMany(array $models, Collection $results, $relation, $type)
  275. {
  276. $dictionary = $this->buildDictionary($results);
  277.  
  278. // Once we have the dictionary we can simply spin through the parent models to
  279. // link them up with their children using the keyed dictionary to make the
  280. // matching very convenient and easy work. Then we'll just return them.
  281. foreach ($models as $model)
  282. {
  283. $key = strtolower($model->getAttribute($this->localKey));
  284.  
  285. if (isset($dictionary[$key]))
  286. {
  287. $value = $this->getRelationValue($dictionary, $key, $type);
  288.  
  289. $model->setRelation($relation, $value);
  290. }
  291. }
  292.  
  293. return $models;
  294. }
  295.  
  296. /**
  297. * Build model dictionary keyed by the relation's foreign key.
  298. *
  299. * @param IlluminateDatabaseEloquentCollection $results
  300. * @return array
  301. */
  302. protected function buildDictionary(Collection $results)
  303. {
  304. $dictionary = array();
  305.  
  306. $foreign = strtolower($this->getPlainForeignKey());
  307.  
  308. // First we will create a dictionary of models keyed by the foreign key of the
  309. // relationship as this will allow us to quickly access all of the related
  310. // models without having to do nested looping which will be quite slow.
  311. foreach ($results as $result)
  312. {
  313. $dictionary[$result->{$foreign}][] = $result;
  314. }
  315.  
  316. return $dictionary;
  317. }
  318.  
  319. }
  320.  
  321. $this->belongsToCI('Model');
  322.  
  323. $this->hasManyCI('Model');
  324.  
  325. $this->hasOneCI('Model');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement